4
s = "Bob hit a ball!, the hit BALL flew far after it was hit."

I need to get rid of the following characters from s

!?',;.

How to achieve this with re.sub?

re.sub(r"!|\?|'|,|;|."," ",s) #doesn't work. And replaces all characters with space

Can someone tell me what's wrong with this?

Wkhan
  • 95
  • 3
  • 11
  • Are you really using Python 2? – AMC Mar 14 '20 at 16:23
  • 1
    Does this answer your question? [Remove specific characters from a string in Python](https://stackoverflow.com/questions/3939361/remove-specific-characters-from-a-string-in-python) – AMC Mar 14 '20 at 16:24

1 Answers1

9

The problem is that . matches all characters, not the literal '.'. You want to escape that also, \..

But a better way would be to not use the OR operator |, but simply use a character group instead:

re.sub(r"[!?',;.]", ' ', s)
petezurich
  • 9,280
  • 9
  • 43
  • 57
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • re.sub(r"!|\?|'|,|;|\."," ",s) I forgot that . is a match all character. Thanks I have added a backslack to the '.' It works fine now – Wkhan Mar 14 '20 at 16:05
  • 1
    @Wkhan Please consider using the second option I provided, it is more readable and much more efficient (only 60 steps vs. 338 steps!) see [here](https://regex101.com/r/haaFjr/1) – Tomerikoo Mar 14 '20 at 16:13