Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19k points)

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?

1 Answer

0 votes
by (106k points)

Yes, Python regular expression has a function known as re.sub which will let the Python recognize the regular expression 

import re

s = "Hello World"

replace= re.sub('[ES]', 'a', s)

print(replace)

image

Related questions

0 votes
1 answer
+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jan 3, 2021 in SQL by Appu (6.1k points)
0 votes
1 answer

Browse Categories

...