[英]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.