Consider a basic regex like a(.+?)a
. How can one replace all occurences of that regex in a file with the content of the first group?

- 11,127
- 7
- 49
- 77
-
This python tutorial site might be helpful: http://pythontesting.net/python/regex-search-replace-examples/ – Mr-IDE Jun 13 '19 at 09:29
3 Answers
Use can use the re
module to use regular expressions in python and the fileinput
module to simply replace text in files in-place
Example:
import fileinput
import re
fn = "test.txt" # your filename
r = re.compile('a(.+?)a')
for line in fileinput.input(fn, inplace=True):
match = r.match(line)
print match.group() if match else line.replace('\n', '')
Before:
hello this
aShouldBeAMatch!!!!! and this should be gone
you know
After:
hello this
aShouldBeAMa
you know
Note: this works because the argument inplace=True
causes input file to be moved to a backup file and standard output is directed to the input file, as documented under Optional in-place filtering.
-
3As a note to anyone who may be confused, `print` statements within that for loop are redirected to the file. (The [documentation](http://docs.python.org/library/fileinput.html) for `fileinput` is quite good). – David Cain Aug 10 '12 at 08:50
-
1This works as intented, although im using `r.sub()` on the line to keep the non-matched parts of the line. – Sebastian Hoffmann Aug 10 '12 at 09:16
-
what is line.replace('\n', '') doing?? Where is the replacement text? Please explain your code – john k Dec 14 '21 at 22:39
sed
Are you limited to using Python tools? Because sed
works very well.
$ sed -i <filename> "s/a(.+?)a/\1/g"
Vim
In a Vim window, give the following search-and-replace ex command:
:%s/\va(.+?)a/\1/g
Note that many regex characters are escaped in Vim- \v
sets "very magic" mode, which removes the need for escaping. The same command with "magic" (the default) is :%s/a\(.\+\?)a/\1/g
Python
If you're looking to do this in Python, BigYellowCactus' answer is excellent (use the re
module for regex, and fileinput
to modify the file).

- 16,484
- 14
- 65
- 75
-
-
@Paranaix, you have [options to run `sed` on Windows](http://stackoverflow.com/a/127567/815632). Alternatively, you could perform a search and replace in Vim. – David Cain Aug 10 '12 at 08:46