Saucy Goat
Saucy Goat

Reputation: 1675

Python - Replacing repeated consonants with other values in a string

I want to write a function that, given a string, returns a new string in which occurences of a sequence of the same consonant with 2 or more elements are replaced with the same sequence except the first consonant - which should be replaced with the character 'm'.

The explanation was probably very confusing, so here are some examples:

I looked into using regex but wasn't able to achieve what I wanted. Any help is appreciated.

Thank you in advance!

Upvotes: 0

Views: 232

Answers (1)

Green Cloak Guy
Green Cloak Guy

Reputation: 24691

Regex is probably the best tool for the job here. The 'correct' expression is

test = """
hello world
Hannibal
error
although
bbb
"""

output = re.sub(r'(.)\1+', lambda g:f'm{g.group(0)[1:]}', test)
# '''
# hemlo world
# Hamnibal
# emror
# although
# mbb
# '''

The only real complicated part of this is the lambda that we give as an argument. re.sub() can accept one as its 'replacement criteria' - it gets passed a regex object (which we call .group(0) on to get the full match, i.e. all of the repeated letters) and should output a string, with which to replace whatever was matched. Here, we use it to output the character 'm' followed by the second character onwards of the match, in an f-string.

The regex itself is pretty straightforward as well. Any character (.), then the same character (\1) again one or more times (+). If you wanted just alphanumerics (i.e. not to replace duplicate whitespace characters), you could use (\w) instead of (.)

Upvotes: 2

Related Questions