Python - regex to remove a if it occurs after b

I want to remove all dots in the text that appears after the vowel character. How can i do this?

Here is the code I would like:

string = re.sub('[aeuio]\.', '[aeuio]', string) 

A meaning similar to observing a vowel that you match and delete. next to him.

+7
python string regex
source share
1 answer

Grab the vowel and replace its backreference:

 import re s = "Se.hi.mo." s = re.sub(r'([aeuio])\.', r'\1', s) print(s) # => Sehimo 

See the Python demo and the regex demo .

Here ([aeuio]) forms the group > and \1 in the replacement template, a numbered ([aeuio]) to the text captured in group 1.

Note the use of the original string literals, where the backslash does not form an escape sequence: r'\1' = '\\1' .

+4
source share

All Articles