1

I have this regular expression

url\s*\((?:\'|"|)((\S*|\?)(?<!\'|\"))(?=\'\)|"\)|\))

It works well for what I want in these cases:

url("../../Common/common/fonts/lato/lato-black-webfont.svg?#lato_blackregular")

But when I have a situation like this it does not work the way I want it

lato_blackregular;src:url("../../Common/common/fonts/lato/lato-black-webfont.eot");src:url("../../Common/common/fonts/lato/lato-black-webfont.eot?#iefix")

How can I define ";" such as a line break, whenever you have a ";" it for the combination and start over?

Demonstration of what is happening: https://regex101.com/r/2YHl2C/1/

Use \S*? does not work because it stops combining correctly with background:url(/uploads/2019/03/0002-image(thumbnail_product).jpg)

Bruno Andrade
  • 565
  • 1
  • 3
  • 17

2 Answers2

2

You may use

url\s*\(['"]?(.*?)['"]?\)(?![^;\s])

See the regex demo.

The pattern will match substrings starting with url(, then there may be an optional single or double quotation mark, then will capture into Group 1 zero or more chars up to the first ) followed with ; or whitepsace/end of string.

See the Regulex graph:

enter image description here

Details

  • url - an url string
  • \s* - 0+ whitespace chars
  • \( - a ( char
  • ['"]? - 1 or 0 quotation marks
  • (.*?) - Group 1: any zero or more chars other than line break chars, as few as possible
  • ['"]? - 1 or 0 quotation marks
  • \) - a ) char
  • (?![^;\s]) - there must be ;, whitespace or end of string immediately to the right of the current location.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

While you already have your answer, what about the imo cleaner (recursive) way:

url
(\(
    (?P<url>(?:[^()]*|(?1))+)
\))

This only needs a tenth of the steps needed by @Wiktor's expression (~4000 vs 400), see your modified demo on regex101.com.

Jan
  • 42,290
  • 8
  • 54
  • 79
  • 1
    That sounds good too. The difference to @Wiktor is that his does not get the quotes. Which is the result I wanted. A group with only the url path – Bruno Andrade Apr 10 '19 at 21:36