Python Regex MULTILINE parameter not working correctly?

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 $.

+4
source share
2 answers

Change the replacement to:

 replace = r'\g<1>' + newVersion + r'\2' 

The problem you are facing is your version:

 replace = "\\101.20.01\\2" 

which misleads the subtitle since there is no field 101. From the documentation for Python re module :

\ g <number> uses the appropriate group number; Therefore, \ g <2> is equivalent to \ 2, but is not ambiguous in substitution, such as \ g <2> 0. \ 20 will be interpreted as a link to group 20, not a link to group 2, followed by a literal character "0".

+6
source

if you do print replace , you will see the problem ...

 replace == '\\101.20.01\2' 

and since you do not have the 101st match, the first part of your line will be lost. Try instead:

 newVersion = "_01.20.01" find = r'^(\s+NOTICE_TYPE revision\[\] = "A_X1)_\d\d+\.\d\d+\.\d\d+(";)$' replace = "\\1" + newVersion + "\\2" 

(moves part of the match so that there is no conflict)

+2
source

All Articles