I have a string - for example, "Hello Worrld". Notice the "r" letter repeats twice.
I wish to identify letters that occur consecutively (two or more times) and retain only one of them. That is, I wish to get "Hello World" with a single "r".
Golang does not seem to have lookahead regular expression.
I tried using the following regular expression to identify letters that repeat consecutively -
r := regexp.Compile(`(.)\1`)
But it selects both the occurrences of the letters - I would want only one of them to be selected.
You could use pcre bindings, however if you just want to remove dup letters you could use strings.Map
, for example:
func stripDups(s string) string {
var last rune
return strings.Map(func(r rune) rune {
if r != last {
last = r
return r
}
return -1
}, s)
}
playground
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