Using substring twice in regex

Firstly, this question may have been asked before, but I'm not sure which phrase to look for.

I have a line:

Maaaa

I have a template:

aah

I would like to combine twice, giving me the starting indices 1 and 2. But, of course, I get only one match (starting index 1), because the regex engine absorbs all 3 "a" and cannot use them again, leaving me 1 "a" which does not match.

How to solve this?

Thanks!

+4
source share
2 answers

You can use the lookahead statement to find a and then

a(?=aa) 
+9
source

The man perlre offers:

  my @a; "Maaaa" =~ /aaa(?{push @a,$&})(*FAIL)/; print join "\n",@a; print "\n"; 

what gives

 aaa aaa 
0
source

Source: https://habr.com/ru/post/1299163/


All Articles