我需要基于正则表达式在 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 开头的字符串的结束位置并以 ) 结束。
- 正则表达式应该是什么?
- 如何在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/