http://www.perlmonks.org?node_id=806118


in reply to Recursive substitution

I like to take a very simple approach to such things, since the tricky methods usually confuse me when I come back to it 6 months later and have to modify it somehow. :-)

my $s = 'ab'; my $pat = qr/^(a{1,3})(?=b)/; while( $s =~ m/$pat/ ) { $s =~ s/$pat/$1a/g; }

Or, if you prefer more of the 1-liner feel:

my $s = 'ab'; my $pat = qr/^(a{1,3})(?=b)/; do{ $s =~ s/$pat/$1a/g } while $s =~ m/$pat/;

I'm sure it is possible to do this in the regex itself (probably with /e). I'm sure other monks will enlighten both of us.

Update: I changed approaches mid-code and forgot to remove the superfluous m//. These solutions are not very perlish.