Regular expression to stop at first match

You need to make your regular expression lazy/non-greedy, because by default, "(.*)" will match all of "file path/level1/level2" xxx some="xxx".

Instead you can make your dot-star non-greedy, which will make it match as few characters as possible:

/location="(.*?)"/

Adding a ? on a quantifier (?, * or +) makes it non-greedy.

Note: this is only available in regex engines which implement the Perl 5 extensions (Java, Ruby, Python, etc) but not in "traditional" regex engines (including Awk, sed, grep without -P, etc.).


location="(.*)" will match from the " after location= until the " after some="xxx unless you make it non-greedy.

So you either need .*? (i.e. make it non-greedy by adding ?) or better replace .* with [^"]*.

  • [^"] Matches any character except for a " <quotation-mark>
  • More generic: [^abc] - Matches any character except for an a, b or c

How about

.*location="([^"]*)".*

This avoids the unlimited search with .* and will match exactly to the first quote.

Tags:

Regex