首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于从HTML字符串中获取srcattribute的正则表达式

用于从HTML字符串中获取srcattribute的正则表达式
EN

Stack Overflow用户
提问于 2016-08-02 14:45:47
回答 1查看 19关注 0票数 2

在我字符串中,它可能包含一个/多个要获取src值链接的"src"..I。

字符串类似于:

代码语言:javascript
复制
"<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> "

或者可能像这样:

代码语言:javascript
复制
 "<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> "

我所做的是:

代码语言:javascript
复制
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 value..but当string contactin多个src值时,它只返回一个src值。

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

EN

回答 1

Stack Overflow用户

发布于 2016-08-02 15:12:07

只需使用以下代码:

代码语言:javascript
复制
             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());
                  }

正则表达式不是一种好的做法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38713451

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档