Simplified example: consider the string aabaabaabaabaacbaabaabaabaa
I want to match all aa occurrences only after the c in the middle, using one regex expression.
The closest I’ve come to is c.*\Kaa but it only matches the last aa, and only the first aa with the ungreedy flag.
I’m using the regex101 website for testing.
>Solution :
You can use
(?:\G(?!^)|c).*?\Kaa
See the regex demo. Details:
(?:\G(?!^)|c)– either the end of the previous successful match (\G(?!^)) or (|) acchar.*?– any zero or more chars other than line break chars, as few as possible\K– forget the text matched so faraa– anaastring.