in reply to Re: Re: Replace zero-width grouping?
in thread Replace zero-width grouping?
I tested the code before posting it. It works correctly. It turns out that it works incredibly inefficiently - the longer test string shows that this loops many more times than necessary. I accidentally had the substr() falling off the end and so it had to restart a lot. I've fixed it up and now its super-slick. The substr() always starts right after the position replaced by the 'A'. This will walk the input string and never covers the same ground twice.
# The instrumented version my $a = "17341234173412341734123417341234"; $expected = "A7AAB2BBA7AAB2BBA7AAB2BBA7AAB2BB"; $r = 0; 1 while substr($a,$r) =~ s/(?{print ">".pos()."$r\n"})(.)(...)\1(?{$r+ +=pos()-4;print"#$r\n"})/A$2B/; print "$a\n$expected\n"; print $a eq $expected ? "Ok\n" : "Failed\n"; # The clean version my $a = "17341234173412341734123417341234"; $r = 0; 1 while substr($a,$r) =~ s/(.)(...)\1(?{$r+=pos()-4;})/A$2B/; print "$a\n"
|
|---|