gpt4 book ai didi

java - 从 HTML 字符串中获取 srcattribute 的正则表达式

转载 作者:行者123 更新时间:2023-11-28 02:52:42 25 4
gpt4 key购买 nike

在我的字符串中它可能包含单个/多个“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 接触多个 src 值时,它只返回一个 src 值..

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

最佳答案

只需使用这段代码:

             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());
}

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

关于java - 从 HTML 字符串中获取 srcattribute 的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38713451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com