Use a sub-string twice in regular expressions

First of all, this question may have been asked before, but I am not sure what phrase to search for.

I have a string:

Maaaa

I have a pattern:

aaa< /p>

I want to match twice and give me starting indexes 1 and 2. But of course I only get one match (starting index 1) because the regular expression engine swallows all 3″a “And can’t use them again to leave me a mismatched “a”.

How can I solve this problem?

Thank you!

You can use lookahead assertions to find a followed by 2 a

a(?=aa)

First of all, this question may have been asked before, but I am not sure what phrase to search for. < p>

I have a string:

Maaaa

I have a pattern:< /p>

aaa

I want to match twice and give me starting indexes 1 and 2. But of course I only get one Match (starting index 1), because the regular expression engine swallows all 3 “a”s and cannot use them again to leave me a mismatched “a”.

How can I solve this problem?

Thank you!

You can use lookahead assertions to find a followed by 2 a

a(?= aa)

Leave a Comment

Your email address will not be published.