I would like to start by saying that this regex does not cover 100% of your cases, but is correct in almost all of them. And I honestly don’t see much way to cover without complicating drastically and maybe using code by hand.
But let’s start with the regex itself:
([^rs])(?=\1+)|(rr)(?=r+)|(ss)(?=s+)
See in regex101
Explanation:
([^rs]) - Qualquer letra que não r ou s
(?=\1+) - Que se repita uma vez ou mais
|(rr) - Ou dois r's
(?=r+) - Que tenham mais r's à frente
|(ss) - Ou dois s's
(?=s+) - Que tenham mais s's à frente
And does the substitution for nothing, empty text, because what is captured are the duplicated letters that you want to remove.
Testing:
Entree:
oiiiiiiii amiggggos passssado Carrrrrrrros
Exit:
oi amigos passado Carros
You can always adjust the regex to other letters you want to let duplicate by moving [^rs]
and in groups (rr)
, adding others you want.
Notice if you step in Carross
regex cannot understand that it was supposed to be Carros
, but this can complicate and well.
There is the possibility of
r
ors
appear more than 2 times ? If yes what is the procedure in this case– Isac
If this happens, it can be considered only 2, and the rest removed. Ex:
carrrro = carro
– Thiago R.
@Thiagor. What have you tried?
– Francisco