A slightly odd question for you all - I have solved my issue of wishing to replace all repeating characters in a string, but I don't really understand my solution. Example is:
txt <- "haarbbbbbbijjjjjan"
gsub("([a-z])\\1+", "\\1", txt)
[1] "harbijan"
Is this just matching all repeated instances of each letter (search term + repeats of search term) and replacing them with the searched for letter? Or is this doing something unintended that I don't fully grasp?
You've declared one group - any symbol between a
and z
. \\1
references this group. Any number of repeatances of this group is substituted into the group value. For example, if group is a
, then any number of a
s will be replaced with group value, e.g. with a
.
Hope I made myself clear =)
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