'regex - multiple matches after a specific word
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 1:[1]
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.
Solution 2:[2]
Just match
aa(?!.*c)
(?!.*c) is a *negative lookahead that, after matching 'aa', 'c' does not appear later in the string.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wiktor Stribiżew |
| Solution 2 | Cary Swoveland |
