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?
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)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments