![](/img/trans.png)
[英]Regex to match empty or any string except a specific string for request mapping
[英]java regex match any string, specific string , any string, specific string and any string
我需要在基于正则表达式的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开头并以)结尾的字符串的结束位置。
我会做这样的事情来找到网址:
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([^)]+)\\)");
如果您只想拥有域部分之后的内容
另一个选择是这样的:
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;'>
<div style='background-image: url(http://www.mydomain.com/images/test.jpg); background-repeat: no-repeat; background-attachment: scroll; height: 400px;'>
组#1 = test.jpg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.