I have a text file and every time that the word "get" occurs I need to insert an @
sign after it.
In Python how do I add a character after a specific word using regex? Right now I am parsing the line word by word and I don't understand regex enough to write the code.
We first use the string. find() method to get the substring index in the string, after which we need to insert another string. After getting the substring index, we split the original string and then concatenate the split strings and the string we need to insert using the + operator to get the desired string.
Inside a character range, \b represents the backspace character, for compatibility with Python's string literals. Matches the empty string, but only when it is not at the beginning or end of a word.
The Difference Between \s and \s+ For example, expression X+ matches one or more X characters. Therefore, the regular expression \s matches a single whitespace character, while \s+ will match one or more whitespace characters.
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
Use re.sub()
to provide replacements, using a backreference to re-use matched text:
import re
text = re.sub(r'(get)', r'\1@', text)
The (..)
parenthesis mark a group, which \1
refers to when specifying a replacement. So get
is replaced by get@
.
Demo:
>>> import re
>>> text = 'Do you get it yet?'
>>> re.sub(r'(get)', r'\1@', text)
'Do you get@ it yet?'
The pattern will match get
anywhere in the string; if you need to limit it to whole words, add \b
anchors:
text = re.sub(r'(\bget\b)', r'\1@', text)
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