I am trying to implement a regexp that, given a string, it checks for a sequence of at least 3 of identical characters and replaces it with two of that character. For example, I want to turn the below string:
sstttttrrrrrrriing
into
ssttrriing
I am thinking of something along the lines of...
$string =~ s/(\D{3,})/substr($1, 0, 2)/e;
But this will not work because:
Can anyone help me?
You can use a capture group and backreference it with \1
, then insert it twice afterwards.
$ perl -plwe 's/(.)\1{2,}/$1$1/g'
sstttttrrrrrrriing
ssttrriing
Or you can use the \K
(keep) escape sequence to avoid having to re-insert.
s/(.)\1\K\1+//g
Replace wildcard .
for any suitable character (class) if needed. For example for letters:
perl -plwe 's/(\pL)\1\K\1+//g'
$ echo "sssssttttttrrrrriiiinnnnggg" | perl -pe "s/(.)\1+/\1\1/g"
ssttrriinngg
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With