Reputation: 422
I have a string looking like this: Hello @StackOverflow! How are you today? I'd like to !sh @StackExchange
I would like it to look like this: Hello ! How are you today? I'd like to !sh
I would like to remove @
and anything after it, until the string is cleared of all matches.
The solution I came up with only removes the first occurence.
re.sub('@\S+ ', '', myString)
Upvotes: 1
Views: 61
Reputation: 785196
You may use this re.sub
:
@\w+\s*
Code:
>>> s = "Hello @StackOverflow! How are you today? I'd like to !sh @StackExchange"
>>> print ( re.sub(r'@\w+\s*', '', s) )
Hello ! How are you today? I'd like to !sh
RegEx Details:
@
: Match literal @
:\w+\s*
: Match 1+ word characters followed by 0 or more whitespacesUpvotes: 1
Reputation: 627
You just need to remove the trailing space in your string.
import re
myString = "Hello @StackOverflow! How are you today? I'd like to !sh @StackExchange"
re.sub('@\S+', '', myString)
Upvotes: 1