Home > Back-end >  Detect and replace multiple letters with single letter
Detect and replace multiple letters with single letter

Time:08-07

Context: In our language (Bahasa - Indonesian language), we tend to repeat letter to stress on something. For example, when we are happy, we say hore. But when we are very happy, we say horeeeeee.

I want to detect and remove this letter repetition. I am using this to detect if a letter is repeated 3 times or more

a = a.replace('(.)\1{3,}', '(.)')

However, this does not work. The repetition is detected but it is not replaced with one letter. Please let me know how to do it.

Thanks

CodePudding user response:

As far as I can tell, python's string replace method does not support regex replacement.

You will want to use the built-in re module.

import re
a=re.sub('(.)\\1{3,}', '\\1',a)

Or if you use raw strings:

import re
a=re.sub(r'(.)\1{3,}', r'\1',a)

CodePudding user response:

You could use the groupby() function from itertools, for example

import itertools

s= 'apa kabarrrrr mass asal manaaaaa'
print(''.join(ch for ch, g in itertools.groupby(s)))

Output

apa kabar mas asal mana

To see the groups for each char one can use list(g)

for k, g in itertools.groupby(s):
     print(k,list(g))

a ['a']
p ['p']
a ['a']
  [' ']
k ['k']
a ['a']
b ['b']
a ['a']
r ['r', 'r', 'r', 'r', 'r']
  [' ']
m ['m']
a ['a']
s ['s', 's']
  [' ']
a ['a']
s ['s']
a ['a']
l ['l']
  [' ']
m ['m']
a ['a']
n ['n']
a ['a', 'a', 'a', 'a', 'a']

CodePudding user response:

Agree with @Michael Sohnen's Answer, just to add, you can also use function to custom replace:

import re


str = 'apa kabarrrrr mass asal manaaaaa';

def replace_func(matches):
    return matches.group()[0];

print(re.sub('(.)\\1{3,}',  replace_func, str)) #apa kabar mass asal mana



This also expands the possibility of customization

  • Related