gpt4 book ai didi

java正则表达式匹配任何字符串,特定字符串,任何字符串,特定字符串和任何字符串

转载 作者:行者123 更新时间:2023-12-02 00:26:43 26 4
gpt4 key购买 nike

我需要基于正则表达式在 HTML 文本中查找图片位置。

例如

HTML 字符串是:

    <div style='background-image: url(http://www.mydomain.com/images/test.jpg); 
background-repeat: no-repeat; background-attachment: scroll; height: 400px;'>

我需要定义一个正则表达式来查找以 http://www.mydomain.com 开头的字符串的结束位置并以 ) 结束。

  1. 正则表达式应该是什么?
  2. 如何在java中找到结束位置?

最佳答案

我会做这样的事情来查找网址:

String input = "<div style='background-image: url(http://www.mydomain.com/images/test.jpg); \n" +
"background-repeat: no-repeat; background-attachment: scroll; height: 400px;'>";

Pattern pattern = Pattern.compile("image:\\surl\\(([^)]+)\\)");
Matcher matcher = pattern.matcher(input);
if (matcher.find()){
String url = matcher.group(1);
System.out.println(url);
}

Pattern pattern = Pattern.compile("image:\\surl\\(http://www\\.mydomain\\.com([^)]+)\\)");

如果您只想包含域部分后面的内容

关于java正则表达式匹配任何字符串,特定字符串,任何字符串,特定字符串和任何字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9887711/

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