snow_leopard
snow_leopard

Reputation: 1556

python - Re module leaving space for matched string

I am using re module in python to remove occurence of certain string. Below is what I am trying:

>>> import re
>>> t = re.sub(re.compile('ab'), "", 'This is a ab text')
>>> t
'This is a  text'

Note that in above instead of replacing 'ab' with '' (nothing) its replacing it with a space. Can someone please suggest whats the issue here ?

Upvotes: 0

Views: 52

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522109

The replacement in fact is empty string, but because ab is surrounded by spaces on both sides, it appears that the replacement has a space. Try this version:

t = re.sub(r'\s*ab\s*', " ", 'This is a ab text')
print(t)

This is a text

The above pattern \s*ab\s* matches and consumes ab along with any surrounding spaces, and then replaces with just a single space.

For the edge case where ab might be the very first or last word in a string, I recommend using strip(), e.g.

t = re.sub(r'\s*ab\s*', " ", 'ab tests can be so boring ab').strip()
print(t)

tests can be so boring

Upvotes: 5

Related Questions