Not as easy as it seems at first glance, since there is no “no” in RE, except for ^ inside [ ] , which matches only one character (as you found). Here is my solution:
import re def subit(m): stuff, word = m.groups() return ("_" * len(stuff)) + word s = 'I am going home now, thank you.'
gives:
_____going_________________you_
To explain. RE itself (I always use raw strings) matches one or more characters ( .+ ), But is not greedy ( ? ). This is fixed in the first group of parentheses (brackets). This is followed by either "go" or "you" or the end of the line ( $ ).
subit is a function (you can call it anything) that is called for each lookup. The matching object is passed from which we can get the captured groups. The first group, we just need the length, since we replace each character with an underscore. The returned string is replaced with what matches the pattern.
source share