简体   繁体   中英

Regular expression for matching repeated substring

I need to get URLs from background-image value in HTML style parameter, in this stage I have this regular ( URL is long regular matching valid URLS so I omit it here for simplification):

background-image\s*?\:\s*?(url\(\s*?(['"])?\s*?(URL)\s*?(\2)?\s*?\)([,]?))+

It matches only the first occurrence of URL, I think I've allowed to match all occurrences (but obviously I haven't). What am I doing wrong?

Input may looks like this:

String txt = "<div style=\"background-image: url('A'), url(B);\">fooo</div>";

and what I need to achieve with my regular:

  1. Check whether there is a background-image value followed with * spaces, then : (colon) and again * spaces.
  2. Extract all values in url() pattern.

Now I am able to to get all values in url() pattern but I am not able to ensure that there is a background-image value.

Your regex is fine, except for that it doesn't search for URL's it searches for the text URL. I've added a \\d behind URL to demonstrate that your regex works:

Pattern p = Pattern.compile("background-image\\s*?\\:\\s*?(url\\(\\s*?(['\"])?\\s*?(URL\\d)\\s*?(\\2)?\\s*?\\)([,]?))+");
Matcher m = p.matcher("background-image: url(URL1); background-image: url(URL2)");
while( m.find() ){
    System.out.println(m.group(3));
}

Output: 
URL1
URL2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM