这是原始的
<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

解决方案 »

  1.   

    http://topic.csdn.net/u/20080823/14/11ad551a-e0ab-4e02-89d8-d87796504ff4.html 
      

  2.   

    <img[^<>]+src="(?:file://|http://)[\w ./-]+"[^<>]*/>
    "<img[^<>]+src="(?:file://|http://)[\\w ./-]+"[^<>]*/>"
      

  3.   

    "<img[^ <>]+src="(?:file:// &brvbarhttp://)[\\w ./-]+"[^ <>]*/>"
      

  4.   

    重贴一下:import java.util.regex.*;public class RegxImgTag { public static void main(String[] args) {
    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标签");
    }}
      

  5.   

    再改一下:import java.util.regex.Matcher;
    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.   

     "<img  src=\"http://sina/sina.gif\" />"主要就是看字符串最短的那个如何匹配
    6#的应该没问题吧