java – 正则表达式中的双引号
发布时间:2020-07-09 02:37:41 所属栏目:Java 来源:互联网
导读:如何使用正则表达式获得双引号内的字符串? 我有以下字符串: img src=http://yahoo.com/img1.jpg alt= 我想把字符串http://yahoo.com/img1.jpg alt =“”外面. 如何使用正则表达式? 我不知道你为什么要使用alt标签,但是这个regexp可以做到你想要的: 组1是
如何使用正则表达式获得双引号内的字符串? 我有以下字符串: <img src="http://yahoo.com/img1.jpg" alt=""> 我想把字符串http://yahoo.com/img1.jpg alt =“”外面. 解决方法我不知道你为什么要使用alt标签,但是这个regexp可以做到你想要的:组1是网址,组2是alt标签.如果img和src之间可以有多个空格,我可能会修改正则表达式,如果在’=’之间可以有空格 Pattern p = Pattern.compile("<img src="([^"]*)" (alt="[^"]*")>"); Matcher m = p.matcher("<img src="http://yahoo.com/img1.jpg" alt=""> " + "<img src="http://yahoo.com/img2.jpg" alt="">"); while (m.find()) { System.out.println(m.group(1) + " " + m.group(2)); } 输出: http://yahoo.com/img1.jpg alt="" http://yahoo.com/img2.jpg alt="" (编辑:莱芜站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |