Bharthan
Bharthan

Reputation: 1512

replace a substring in python using regular expression

I have a test file like:

rd20140921
rd20131122 rd20131122
hello?rd20131122
rd20140921
rd20140921

and my python code to replace any string which is 10 characters long and starts with rd201 with current date is

filesToSearch=["test.txt"];
textToReplace = "rd"+today.isoformat().replace("-","");
print ("Text to replace: %s" % textToReplace);

for file in filesToSearch:
    for line in fileinput.FileInput(file,inplace=1):
        line = re.sub(r"^rd201[0-9]{5}$", textToReplace, line)
        print(line, end='');

this code replace only line 1 4 and 5 not 2 and 3.

Upvotes: 0

Views: 281

Answers (1)

Lix
Lix

Reputation: 47956

Your regular expression is not matching exactly what you think it is. The $ character matches the end of a string so lines 1,4,5 are the only lines that match the expression.

For the string: rd20131122 rd20131122, you match the first rd20131122 but that is not the end of the string, so the match fails.

For the string: hello?rd20131122, it doesn't match your expression that states the string should start with rd201. It starts with hello and so the expression doesn't match it.

If you would like to replace all occurrences of strings that contain the expression rd201[0-9]{5} then all you have to do is remove the string start (^) and string end ($) parts of your regular expression.

Upvotes: 1

Related Questions