Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regex to remove emails from string

Tags:

python

regex

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

like image 260
dokondr Avatar asked May 17 '17 14:05

dokondr


People also ask

How do I extract an email from a list in Python?

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.

How to Validate email using regular expression in Python?

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.


3 Answers

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.

like image 146
Gawil Avatar answered Sep 25 '22 20:09

Gawil


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.

like image 37
Mike Driscoll Avatar answered Sep 23 '22 20:09

Mike Driscoll


out = ' '.join([item for item in inp.split() if '@' not in item])
like image 36
stamaimer Avatar answered Sep 23 '22 20:09

stamaimer