Search code examples
regexpcre

RegEx: Match nth occurrence


I have the following string:

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

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

I tried using _name=(.*?)" but there are other quotes in the string as well. Is there a way to match the 3rd quote? I tried _name=(.*?)"{3} but the {3} matches for the quotes back to back, i.e. """

You can try it here


Solution

  • You can use this regex:

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

    RegEx Demo

    RegEx Details:

    • \b_name: Match full word _name:
    • =: Match a =
    • (?:[^"]*"){3}: Match 0 or more non-" characters followed by a ". Repeat this group 3 times.