Wednesday, 15 February 2017

regex - Regular expression to stop at first match



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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...