Reputation: 1675
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:
"hello world"
should return "hemlo world"
"Hannibal"
should return "Hamnibal"
"error"
should return "emror"
"although"
should return "although"
(returns the same string because none of the characters are repeated in a sequence)"bbb"
should return "mbb"
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
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