Python Forum
Who enjoys Py RegEx? re.sub() isn't working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Who enjoys Py RegEx? re.sub() isn't working
#3
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.

>>> re.sub("Bar", "-", "FooBarBaz")
'Foo-Baz'
>>> re.sub(".*Bar.*", "-", "FooBarBaz")
'-'
Reply


Messages In This Thread
RE: Who enjoys Py RegEx? re.sub() isn't working - by bowlofred - Jun-07-2020, 11:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Regex to extract IPs between () not working mrapple2020 5 3,527 Apr-12-2019, 08:03 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020