3

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?

Sebastian Hoffmann
  • 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 Answers3

7

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.

Jon-Eric
  • 16,977
  • 9
  • 65
  • 97
sloth
  • 99,095
  • 21
  • 171
  • 219
  • 3
    As 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
  • 1
    This 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
-2

You can use Notepad++ with Version >= 6.0. Since then it does support PCRE Regex.

You can then use your regex a(.+?)a and replace with $1

stema
  • 90,351
  • 20
  • 107
  • 135
-4

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

David Cain
  • 16,484
  • 14
  • 65
  • 75