Need to replace emails in a string, so:
inp = 'abc [email protected] 123 any@www foo @ bar 78@ppp @5555 aa@111"
should result in:
out = 'abc 123 foo bar"
What regex to use?
In [148]: e = '[^\@]\@[^\@]'
In [149]: pattern = re.compile(e)
In [150]: pattern.sub('', s)
Out[150]: 'one aom 123 4two'
In [151]: s
Out[151]: 'one ab@com 123 4 @ two'
Does not work for me
To extract emails form text, we can take of regular expression. In the below example we take help of the regular expression package to define the pattern of an email ID and then use the findall() function to retrieve those text which match this pattern.
Method 2: Validate Email Address with Python using re.The re. match() searches only from the beginning of the string and returns the match object if found. But if a match of substring is found somewhere in the middle of the string, it returns none.
Replace :\S*@\S*\s?
by ''
Demo here
Some explanations :\S*
: match as many non-space characters you can@
: then a @\S*
: then another sequence of non-space characters\s?
: And eventually a space, if there is one. Note that the '?' is needed to match an address at the end of the line. Because of the greediness of '?', if there is a space, it will always be matched.
I personally prefer doing string parsing myself. Let's try splitting the string and getting rid of the items that have the @
symbol:
inp = 'abc [email protected] 123 any@www foo @ bar 78@ppp @5555 aa@111'
items = inp.split()
Now we can do something like this:
>>> [i for i in items if '@' not in i]
['abc', '123', 'foo', 'bar']
That gets us almost there. Let's modify it a bit more to add a join
:
>>> ' '.join([i for i in inp.split() if '@' not in i])
'abc 123 foo bar'
It may not be RegEx, but it works for the input you gave.
out = ' '.join([item for item in inp.split() if '@' not in item])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With