Home > Blockchain >  Regex replace multiple letter with single python
Regex replace multiple letter with single python

Time:08-06

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:

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