I am trying to implement a regex that, given a string, checks a sequence of at least 3 identical characters and replaces it with two characters. For example, I want to rotate the line below:
sstttttrrrrrrriing
in
ssttrriing
I think of something like ...
$string =~ s/(\D{3,})/substr($1, 0, 2)/e;
But this will not work, because:
- He does not verify the identity of the three alphabetic characters; it may correspond to a sequence of three or more different characters.
- It replaces only the first match; I need to place all matches in this regex.
Can anybody help me?
source share