Determining the unmatched portion of a string using a regex in Python
Question: Suppose I have a string "a foobar" and I use "^a\s*" to match "a ".

Is there a way to easily get "foobar" returned? (What was NOT matched)

I want to use a regex to look for a _command word_ and also use the regex to
remove the command word from the string.

I know how to do this using something like:

    
    
    mystring[:regexobj.start()] + email[regexobj.end():]
    

But this falls apart if I have multiple matches.

Thanks!


Answer: Use `re.sub`:

    
    
    import re
    s = "87 foo 87 bar"
    r = re.compile(r"87\s*")
    s = r.sub('', s)
    print s
    

Result:

    
    
    foo bar
    

