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
  • 2
  • 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

2 Answers2

7

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
  • 7,683
  • 8
  • 34
  • 51
Tomerikoo
  • 15,737
  • 15
  • 35
  • 52
  • 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
0

no need for regex, u can do it with simple str.replace(), something like this:

chars_to_replace = "!?',;."

def replace(text):
    for char in chars_to_replace:
        if char in text:
            text = text.replace(char, "")

    return text


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


print replace(my_text)

// Bob hit a ball the hit BALL flew far after it was hit
Badjio
  • 540
  • 1
  • 7
  • 12