这是原始的
<img height="129" width="270" alt="" src="http://sina/sina.gif" />现在希望用正则匹配出string里面有多少个下面写法的字符串<img 任意1 src="任意2.gif" />其中任意1 都是任意的东西也可以没有
任意2是文件路径(本地路径或者网络路径)例如:
----------------------
String =
<img height="129" width="270" alt="" src="http://sina/sina.gif" />
<p><body>
<embed src="c:/frist.mp3" height="130" type="application/x-shockwave-media" menu="true" loop="true" play="true"></embed><img src="http://sina/sina.gif" />
-----------则结果为2
<img height="129" width="270" alt="" src="http://sina/sina.gif" />现在希望用正则匹配出string里面有多少个下面写法的字符串<img 任意1 src="任意2.gif" />其中任意1 都是任意的东西也可以没有
任意2是文件路径(本地路径或者网络路径)例如:
----------------------
String =
<img height="129" width="270" alt="" src="http://sina/sina.gif" />
<p><body>
<embed src="c:/frist.mp3" height="130" type="application/x-shockwave-media" menu="true" loop="true" play="true"></embed><img src="http://sina/sina.gif" />
-----------则结果为2
"<img[^<>]+src="(?:file://|http://)[\\w ./-]+"[^<>]*/>"
String test = "<img height=\"129\" width=\"270\" alt=\"\" src=\"http://sina/sina.gif\" />\n" +
"<p> <body>\n" +
"<embed src=\"c:/frist.mp3\" height=\"130\" type=\"application/x-shockwave-media\" menu=\"true\" loop=\"true\" play=\"true\"> </embed>\n" +
"<img src=\"http://sina/sina.gif\" />";
//<img[^<>]+src="(?:file://|http://)[\w ./-]+"[^<>]*/>
final String REGEX = "<img[^<>]+src=\"(?:file://|http://)[\\w ./-]+\"[^<>]*/>";
Pattern p = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(test);
int n = 0;
while (m.find()) n++;
System.out.println("找到" + n + "个匹配的img标签");
}}
import java.util.regex.Pattern;public class RegxImgTag { public static void main(String[] args) {
final String REGEX = "<img\\s+[^<>]*(?<=\\s+)src=\"(?:file://|http://)[\\w ./-]+\"[^<>]*/>";
String test =
"<img height=\"129\" width=\"270\" alt=\"\" src=\"http://sina/sina.gif\" />\n" +
"<p> <body>\n" +
"<embed src=\"c:/frist.mp3\" height=\"130\" type=\"application/x-shockwave-media\" menu=\"true\" loop=\"true\" play=\"true\"> </embed>\n" +
"<img src=\"http://sina/sina.gif\" />";
Pattern p = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(test);
int n = 0;
while (m.find()) n++;
System.out.println("找到" + n + "个匹配的img标签");
}}
6#的应该没问题吧