Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - regex to remove a if it occures after a b

I want to remove all the dots in a text that appear after a vowel character. how can I do that?

Here is the code I wish I had:

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

Meaning like keep whatever vowel you have matched and remove the '.' next to it.

like image 968
yukashima huksay Avatar asked Dec 12 '17 19:12

yukashima huksay


People also ask

How do you ignore a case in regex Python?

re. IGNORECASE : This flag allows for case-insensitive matching of the Regular Expression with the given string i.e. expressions like [A-Z] will match lowercase letters, too. Generally, It's passed as an optional argument to re. compile() .

What does \r represent in regex?

Definition and Usage The \r metacharacter matches carriage return characters.

What is D+ in regex Python?

What is this doing? Well, \D matches any character except a numeric digit, and + means 1 or more. So \D+ matches one or more characters that are not digits. This is what we're using instead of a literal hyphen, to try to match different separators.

What is B in regular expression in Python?

Inside a character range, \b represents the backspace character, for compatibility with Python's string literals.


1 Answers

Capture the vowel and replace with a backreference to it:

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

See the Python demo and a regex demo.

Here, ([aeuio]) forms a capturing group and \1 in the replacement pattern is a numbered backreference referencing the text captured into Group 1.

Mind the usage of raw string literals where a backslash does not form an escape sequence: r'\1' = '\\1'.

like image 86
Wiktor Stribiżew Avatar answered Sep 28 '22 15:09

Wiktor Stribiżew