Regular expression to stop at first match
My regex pattern looks something like
<xxxx location="file path/level1/level2" xxxx some="xxx">
I am only interested in the part in quotes assigned to location. Shouldn't it be as easy as below without the greedy switch?
/.*location="(.*)".*/
Does not seem to work.
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