Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a substring twice in regex

Tags:

regex

First, this question may have been asked before, but I'm not sure what phrase to search on.

I have a string:

Maaaa

I have a pattern:

aaa

I would like to match twice, giving me starting indices of 1 and 2. But of course I only get a single match (start index 1), because the regex engine gobbles up all 3 "a"s and can't use them again, leaving me with 1 "a" which doesn't match.

How do I solve this?

Thanks!

like image 459
djcredo Avatar asked Jan 23 '23 16:01

djcredo


1 Answers

You could use a lookahead assertion to find an a followed by 2 a's

a(?=aa)
like image 151
Paul Creasey Avatar answered Jan 25 '23 23:01

Paul Creasey