0

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 ?

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
snow_leopard
  • 1,466
  • 2
  • 20
  • 36

1 Answers1

5

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
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360