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.
$
at the end of the expression indicates the end of a string.^
indicates the beginning. Line 3 doesn't begin with this. Get rid of those two tokens and it should replace everything.