Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove repeated character between words

Tags:

regex

I am trying out the quiz from Regex 101

In Task 6, the question is

Oh no! It seems my friends spilled beer all over my keyboard last night and my keys are super sticky now. Some of the time when I press a key, I get two duplicates. Can you pppllleaaaseee help me fix this? Content in bold should be removed.

I have tried this regex

([a-z])(\1{2})

But couldn't get the solution.

like image 298
om39a Avatar asked Dec 21 '22 11:12

om39a


2 Answers

The solution for the riddle on that website is:

/(.)\1{2}/g

Since any key on the keyboard can get stuck, so we need to use ..

\1 in the regex means match whatever the 1st capturing group (.) matches.

Replacement is $1 or \1.

The rest of your regex is correct, just that there are unnecessary capturing groups.

like image 175
nhahtdh Avatar answered Dec 31 '22 01:12

nhahtdh


Your regex is correct if you want to match exactly three characters. If you want to match at least three, that is

([a-z])(\1{2,})

or

([a-z])(\1\1+)

Since you don't need to capture anything but the first occurence, these are slightly better:

([a-z])\1{2}   # your original regex (exactly three occurences)
([a-z])\1{2,}
([a-z])\1\1+

Now, the replacement should be exactly one occurence of the character, and nothing more:

\1
like image 43
John Dvorak Avatar answered Dec 30 '22 23:12

John Dvorak