RegEx: match with nth occurrence

I have the following line:

_name=aVlTcWRjVG1YeDhucWdEbVFrN3pSOHZ5QTRjOEJZZmZUZXNIYW1PV2RGOWYrczBhVWRmdVJTMUxYazVBOE8zQ3JNMmNVKzJLM2JJTzFON3FiLzFHUE0xY0pkdz09LS1jbkkwaWoxUUl3YVhMMkhtZHpaOW13PT0"%"3D--57356371d167f" 

I want to match everything between = and end " (note that there are other quotes after that, so I can't just select the last one " ).

I tried using _name=(.*?)" , But there are other quotes on the line. Is there a way to match the third quote? I tried _name=(.*?)"{3} , but {3} matches the quotation marks back, t .e. """

You can try here

+7
regex pcre
source share
2 answers

You can use this regex:

 _name=(?:[^"]*"){3} 

RegEx Demo

+6
source share

If you want to combine everything between the first and third (!) Double quotes (the third is not necessarily the last, you said), you can use a template like this:

 $string = '_name=foo"bar"test" more text"'; // This pattern will not include the last " (note the 2, not 3) $pattern = '/_name=((.*?"){2}.*?)"/'; preg_match($pattern, $string, $m); echo $m[1]; 

Output:

 foo"bar"test 

Original answer:

I'm not sure if I got you right, but it looks like you want to do the so-called greedy match, which means you want to match the string to the last " , regardless of whether the string contains multiple " s.

To fulfill the greedy coincidence, just let go ? , eg:

 _name=(.*)" 

You can try it here: https://regex101.com/r/uC5eO9/2

+1
source share

All Articles