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

How to write a Regex to match all string groups?

For example, find all aa groups in aaa string. This should result in two aa matches, the first one starting at index 0 and the second starting at index 1.

>Solution :

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

You can use positive lookahead to crossmatch all aa strings in the given aaa string. The regex you are looking for is (?=(aa)).

JS Example

console.log(Array.from("aaa".matchAll(/(?=(aa))/g)).map(i => i[1]))

Python Example

import re

re.findall(r"(?=(aa))", "aaa")  # ['aa', 'aa']
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