in reply to Replacing with multiple occurrences.
Is there something similar for replacing, something like...Sure is, just use a bit of back-referencing and an evaluated replacement and you're sitting pretty
See. perlre for more info.my @strs = qw/fooxx baryyyy bazzzzzzz/; s{ ( (.) \2+ ) \z }(substr($1, 0, length($1) / 2))ex, print for @strs; __output__ foox baryy bazaaa
In case you're working on large strings the above regex would be quite hefty in terms of processing, so here's a version that would perform much better on longer strings
This is less processor intensive because it only needs to backtrack once, whereas the first regex has to backtrack all the way through the string.reverse =~ /\A((.)\2+)/ and substr($_, -(length($1) / 2)) = '' for @strs; print map "$_\n", @strs; __output__ foox baryy bazaaa
_________
broquaint
update: added second code example
|
|---|