簡體   English   中英

java regex匹配任何字符串,特定字符串,任何字符串,特定字符串和任何字符串

[英]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開頭並以)結尾的字符串的結束位置。

  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([^)]+)\\)");

如果您只想擁有域部分之后的內容

另一個選擇是這樣的:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM