Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to remove repetitive characters

Tags:

ruby

I have a string like this:

i got sooome screeeewy inpuuuuut

I need to normalize it and beside other processing, I need to remove all repeated chars, i.e. soooome becomes some and so on.

Grammar correctness does not matter.

like image 251
Ocean Moon Avatar asked Aug 31 '26 23:08

Ocean Moon


1 Answers

You do not need regex for this. Simply use str.squeeze:

str = 'i got sooome screeeewy inpuuuuut'
=> "i got sooome screeeewy inpuuuuut"

str.squeeze
=> "i got some screwy input"

See live demo here.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!