正则表达式从HTML字符串获取srcattribute

时间:2016-08-02 06:45:47

标签: java html string

在我的String中它可能包含单个/多个“src”..我想获取那个src值链接..

字符串就像:

"<p>Lets take an example</p> 
<p>
<img alt="\therefore its your address\\"
 src="http://localhost:8080/image?%5Ctherefore%20P%5B1&amp;plus;%5Cfrac%
 %28n-2%29%7D%7B100%7D%5D%3DP%5B1&amp;plus;%5Cfrac%7B4n%7D%7B100%7D%5D%3D930%5C%5C%
 %5C%5C%206n-12%3D4n%5C%5C%202n%3D12%5C%5C%20n%3D6%5C%5C%20%5Ctherefore%20P%5B1&amp;plus
5Cfrac%7B4%5Ctimes6%7D%7B100%7D%5D%3D930%5C%5C%20P%3D%5Cfrac%7B930%5Ctimes100%7D%7B124%7D%5C%5C%20P%3DRs.%5C%20750" /></p> "

或者可能喜欢:

 "<p>Lets take an example</p> <p><img alt="\therefore its your  \\" src="http://http://localhost:8080/image?%5Cthe
 refore%20P%281&amp;plus;%5Cfrac%7B20%7D%7B100%7D%29%5En%
 C%20P%281.2%29%5En%3E2P%5C%5C" /></p> <p>its your another address</p> <p>may be your address is<img alt="aoouern " 
 src="http://http://localhost:8080/image?281.2%29%5E2P%3D1.44P" />
 </p> <p>or its
 <img alt="(1.2)^3P=1.728P" src="http://localhost:8080/image?%281.2%29%5E3P%3D1.728P" />
 </p> <p>or you can do <img alt="lets thake this" 
  src="http://localhost:8080/image?%281.2%29%5E4P%3D2.0736P" /></p> <p>so, your are clear?;</p> "

我所做的是:

String s = "src=";
int ix = solution_box.indexOf(s)+s.length();
String value = solution_box.substring(ix, solution_box.indexOf("\"", ix+1));
value = value.substring(1);
System.out.println(value);

当我的字符串中只有一个“src”时,我可以获取该src值..但是当String contactins多个src值时,它只返回一个src值..

我怎样才能得到多个???我做错了什么?

1 个答案:

答案 0 :(得分:0)

只需使用此代码:

             Pattern p=null;
             Matcher m= null;
             String word0= null;
             String word1= null;

             p= Pattern.compile(".*<img[^>]*src=\"([^\"]*)",Pattern.CASE_INSENSITIVE);
             m= p.matcher(solution_box);
             while (m.find())
                  {
                 word0=m.group(1);
                 System.out.println(word0.toString());
                  }

正则表达不是一个好习惯..