Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Python adding characters after a certain word

Tags:

python

regex

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.

like image 601
English Grad Avatar asked Dec 22 '13 23:12

English Grad


People also ask

How do you add a string after a specific character in Python?

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.

What is \b in Python regex?

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.

What does %s mean in regex?

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.

How do you add a character in regex?

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).


1 Answers

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)
like image 83
Martijn Pieters Avatar answered Sep 24 '22 20:09

Martijn Pieters