Sornpraram Xu
Sornpraram Xu

Reputation: 11

How to use Regular-Expressions find repeated characters in string

I have got a task

"Turn character that is single into '(' and character that are repeated into ')'

for Example "TAAreTheBest" into ")))())()()()"

From above the character that have turn to ')' is T,A,E.

So key is I want to use REGEX to find out which characters is repeated and replace it with ')'

These are code that I have try it before and it doesn't work for me

(\w)\1* 
([a-zA-Z])\1*
\w{2,}

I am very new to python. And I want to learn more about REGEX so I think this task could use regex to solve it. So please help me out. Thank you.

Upvotes: 1

Views: 805

Answers (1)

Askold Ilvento
Askold Ilvento

Reputation: 1520

I hope it is not supposed to be done from one sub

import re

string = 'baTAAreTheBestaaaaabbbbaaaaaaa'

#1 replace chars that occur more then twice
tmp = ''
while tmp != string:
  tmp = string
  string = re.sub(r'(\w)(((.*)\1){2,})', r')\2', tmp)

#2 replace consecutive pairs (dunno why this are not handled by 3rd replace)
string = re.sub(r'(\w)\1', r'))', string)
#3 replace separate pairs
string = re.sub(r'(\w)(.*)\1', r')\2)', string)
#3 replace unique chars
string = re.sub(r'\w', '(', string)
print(string)

Upvotes: 1

Related Questions