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.
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() .
Definition and Usage The \r metacharacter matches carriage return characters.
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.
Inside a character range, \b represents the backspace character, for compatibility with Python's string literals.
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'
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With