Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to stop at first match

Tags:

regex

People also ask

How do I stop regex greedy?

You make it non-greedy by using ". *?" When using the latter construct, the regex engine will, at every step it matches text into the "." attempt to match whatever make come after the ". *?" . This means that if for instance nothing comes after the ".

What is the regular expression for line break?

"\n" matches a newline character.

What is '?' In regular expression?

It means "Match zero or one of the group preceding this question mark." It can also be interpreted as the part preceding the question mark is optional. In above example '?' indicates that the two digits preceding it are optional. They may not occur or occur at the most once.

What does * do in regular expression?

The Match-zero-or-more Operator ( * ) This operator repeats the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern.


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 JavaScript, 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.


Use non-greedy matching, if your engine supports it. Add the ? inside the capture.

/location="(.*?)"/

Use of Lazy quantifiers ? with no global flag is the answer.

Eg,

enter image description here

If you had global flag /g then, it would have matched all the lowest length matches as below. enter image description here