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
aandz.\\1references this group. Any number of repeatances of this group is substituted into the group value. For example, if group isa, then any number ofas will be replaced with group value, e.g. witha.Hope I made myself clear =)