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:
- It doesn’t check if the three alphabetical characters are identical; it can match a sequence of three or more distinct characters.
- It only replaces the first match; I need to accommodate for all matches in this regexp.
Can anyone help me?
You can use a capture group and backreference it with
\1, then insert it twice afterwards.Or you can use the
\K(keep) escape sequence to avoid having to re-insert.Replace wildcard
.for any suitable character (class) if needed. For example for letters: