Search code examples
pythonregexartificial-intelligencewords

Python regex : detect a word with duplicated chars


I wonder if i could detect the word 'hey' but in this form : 'heeeey' How would i do that in python ? I searched many times but i didn't find an answer.

*I want the program to get it like 'hey' at last, i want it to understand that 'heeey' means 'hey'.


Solution

  • import re
    p = re.compile(r'(.)\1+')
    test_str = "heeeey"
    subst = r"\1"
    
    result = re.sub(p, subst, test_str)
    

    You can do it through re.sub.Here we capture any character which are repeated ahead and replace all by \1 .So all the repetitions will be gone.

    See demo.

    https://regex101.com/r/cK4iV0/7#python