我需要找到基于regex的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开头并以)结束的字符串的结束位置。
中找到末端位置?
发布于 2012-03-27 10:31:27
我会做这样的事找到网址:
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([^)]+)\\)");如果您只想拥有以下域部分
发布于 2012-03-27 10:43:20
另一种选择是这样的:
www\\.mydomain\\.com.*/([\\w-\\.]*)在<div style='background-image: url(http://www.mydomain.com/images/test.jpg); background-repeat: no-repeat; background-attachment: scroll; height: 400px;'>上运行时
第1组= test.jpg
https://stackoverflow.com/questions/9887711
复制相似问题