0

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.

2
  • 1
    That is not what your regular expression says... The $ at the end of the expression indicates the end of a string.
    – Lix
    Commented Aug 21, 2014 at 10:24
  • 2
    And ^ indicates the beginning. Line 3 doesn't begin with this. Get rid of those two tokens and it should replace everything.
    – filmor
    Commented Aug 21, 2014 at 10:26

1 Answer 1

1

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.