My regex pattern looks something like
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.
Answer
You need to make your regular expression 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.
No comments:
Post a Comment