535

I have a parameter file of the form:

parameter-name parameter-value

Where the parameters may be in any order but there is only one parameter per line. I want to replace one parameter's parameter-value with a new value.

I am using a line replace function posted previously to replace the line which uses Python's string.replace(pattern, sub). The regular expression that I'm using works for instance in vim but doesn't appear to work in string.replace().

Here is the regular expression that I'm using:

line.replace("^.*interfaceOpDataFile.*$/i", "interfaceOpDataFile %s" % (fileIn))

Where "interfaceOpDataFile" is the parameter name that I'm replacing (/i for case-insensitive) and the new parameter value is the contents of the fileIn variable.

Is there a way to get Python to recognize this regular expression or else is there another way to accomplish this task?

daaawx
  • 3,273
  • 2
  • 17
  • 16
Troy Rockwood
  • 5,875
  • 3
  • 15
  • 20

4 Answers4

713

str.replace() v2|v3 does not recognize regular expressions.

To perform a substitution using a regular expression, use re.sub() v2|v3.

For example:

import re

line = re.sub(
           r"(?i)^.*interfaceOpDataFile.*$", 
           "interfaceOpDataFile %s" % fileIn, 
           line
       )

In a loop, it would be better to compile the regular expression first:

import re

regex = re.compile(r"^.*interfaceOpDataFile.*$", re.IGNORECASE)
for line in some_file:
    line = regex.sub("interfaceOpDataFile %s" % fileIn, line)
    # do something with the updated line
Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
  • 13
    I had to pass in `flags=re.MULTILINE` as the last argument to `re.sub` in order to get this to work, which makes sense - [read about it in the docs here](https://docs.python.org/2/library/re.html#re.MULTILINE) – tobek Mar 11 '16 at 23:26
  • 21
    regex compilations are cached ([docs](https://docs.python.org/3.6/library/re.html#re.compile)), so compiling isn't even necessary. But as you show, if one compiles, compile outside the loop. – alttag Nov 14 '17 at 22:01
  • 1
    For anyone that wants to know the details of whether or not to compile, check [this answer](https://stackoverflow.com/a/452143). – Lual Feb 15 '20 at 21:59
505

You are looking for the re.sub function.

import re
s = "Example String"
replaced = re.sub('[ES]', 'a', s)
print(replaced)

will print axample atring

Hugo
  • 27,885
  • 8
  • 82
  • 98
Jacek Przemieniecki
  • 5,757
  • 1
  • 12
  • 15
20

As a summary

import sys
import re

f = sys.argv[1]
find = sys.argv[2]
replace = sys.argv[3]
with open (f, "r") as myfile:
     s=myfile.read()
ret = re.sub(find,replace, s)   # <<< This is where the magic happens
print ret
kpie
  • 9,588
  • 5
  • 28
  • 50
11

re.sub is definitely what you are looking for. And so you know, you don't need the anchors and the wildcards.

re.sub(r"(?i)interfaceOpDataFile", "interfaceOpDataFile %s" % filein, line)

will do the same thing--matching the first substring that looks like "interfaceOpDataFile" and replacing it.

Nelz11
  • 3,066
  • 1
  • 14
  • 20
  • I need to replace the entire line because the original file will have something like: `interfaceOpDataFile SomeDummyFile.txt` and I will want to replace it with: `interfaceOpDataFile SomeUsefulFile.txt` If I don't include the anchors how will replace know that I want to get rid of `SomeDummyFile.txt`? – Troy Rockwood May 23 '13 at 19:18
  • 2
    Ah, I mis-understood exactly what you were doing with the replacement. If each pair is on its own line, you still don't need the anchors explicity. `re.sub(r"(?i)(interfaceOpDataFile).*", r'\1 UsefulFile', line)` This will take the whole line, capture the arguement name, and add it back the the substitution for you. – Nelz11 May 23 '13 at 20:04