简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM