What I need is to parce my projects’ resource files and change its version number. I have a working JS script but I would like to implement it with python.
So, the problem stands in using re.sub:
version = "2,3,4,5"
modified_str = re.sub(r"(/FILEVERSION )\d+,\d+,\d+,\d+(\s)/g", version, str_text)
I understand that because of using capturing groups my code is incorrect. And I tried to do smth like:
modified_str = re.sub(r"(/FILEVERSION )(\d+,\d+,\d+,\d+)(\s)/g", r"2,3,4,5\2", str_text)
And still no effect. Please help!
That’s not the way to make multiline regexes in python. You have to compile the regex with the MULTILINE flag:
Also, since you’re using re.sub(), the
FILEVERSIONpart of your string will disappear if you don’t specify it again in the replacement string:To match other things than
FILEVERSION, introduce a capture group with an alternation:Then you can inject the captured expression into the replacement string using the backreference
\1: