string - Substitute same number of repeated characters with others using Regex -
I want to replace repeating characters with the second character for the same amount of repeated characters, and if it repeats at the beginning Door's been. Minimum recurrence number should be greater than 2.
For the East: $ string = 'BBBBCIDIE' Here BBBB should be replaced with yyyy
But if $ string = 'bbcccccccddddddeefdfg' should not be replaced first double character As nothing is more than two times
$ string = ~ s / ^ (b) {3,} / y /
will be more than 2b only Only a y
Is it possible to use a regular line expression? Note: If someone replies in Pearl or Python then it would be nice.
You can use code like this:
$ String = 'bbbbccccccccddddddeeeffdfg'; $ String = ~ s / ^ ((\ w) \ 2 {2,}) / 'y' x length ($ 1) / e; Print $ string "\ N";
output
yyyyccccccccddddddeeeffdfg
- execute
/ e
-> For the replacement of code -
'y' x length ($ 1)
-> The character length of the matching group, too many times the letter'y'
# 1 to repeat
Comments
Post a Comment