Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 (|) a c char
  • .*? – any zero or more chars other than line break chars, as few as possible
  • \K – forget the text matched so far
  • aa – an aa string.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading