I am writing a simple update version in Python, and the regex engine gives me big problems.
In particular, ^ and $ do not match correctly even with the re.MULTILINE parameter. The string matches without ^ and $, but not joy.
I would be grateful for your help if you can determine what I am doing wrong.
thanks
target.c
somethingsomethingsomething NOTICE_TYPE revision[] = "A_X1_01.20.00"; somethingsomethingsomething
versionUpdate.py
fileName = "target.c" newVersion = "01.20.01" find = '^(\s+NOTICE_TYPE revision\[\] = "A_X1_)\d\d+\.\d\d+\.\d\d+(";)$' replace = "\\1" + newVersion + "\\2" file = open(fileName, "r") fileContent = file.read() file.close() find_regexp = re.compile(find, re.MULTILINE) file = open(fileName, "w") file.write( find_regexp.sub(replace, fileContent) ) file.close()
Update : Thank you John and Ethan for the valid point. However, regexp still does not match if I save $. It works again as soon as I delete $.
source share