Jun-07-2020, 11:43 PM
1 and 2 only are the same if the match is the whole string. If the match is a substring, then the portion of the string that wasn't matched isn't touched.
For index1, "Projects....md" is the whole string. So when the sub happens, the string returned is just the matching groups.
For index3, "Bodywork....md" is the second half of the string. That part is removed and replaced with the groups, and the initial part of the string (everything before "Bodywork") is left in place.
It's possible that you could extend your match to the entire string by adding a "^.*" to the start of your match. But I haven't looked to see if that would cause any other problems.
For index1, "Projects....md" is the whole string. So when the sub happens, the string returned is just the matching groups.
For index3, "Bodywork....md" is the second half of the string. That part is removed and replaced with the groups, and the initial part of the string (everything before "Bodywork") is left in place.
It's possible that you could extend your match to the entire string by adding a "^.*" to the start of your match. But I haven't looked to see if that would cause any other problems.
>>> re.sub("Bar", "-", "FooBarBaz") 'Foo-Baz' >>> re.sub(".*Bar.*", "-", "FooBarBaz") '-'