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 ".
"\n" matches a newline character.
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.
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>[^abc]
- Matches any character except for an a, b or cHow 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,
If you had global flag /g
then, it would have matched all the lowest length matches as below.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With