8

Need to replace emails in a string, so:

inp = 'abc user@xxx.com 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

dokondr
  • 3,389
  • 12
  • 38
  • 62
  • I would suggest splitting the string on whitespaces, removing the elements of the array containing an @ and merging the string. But it is not a regex – fonfonx May 17 '17 at 14:41
  • Possible duplicate of [Using a regular expression to validate an email address](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address) – logi-kal May 17 '17 at 14:44
  • @fonfonx Yes, I was thinking about splitting, removing and merging back also. But it seems overkill to me. – dokondr May 17 '17 at 14:47
  • actually example in question is wrong, any@www 78@ppp @5555 are not valid email and so they should not be removed. – Alok Prasad Nov 22 '20 at 16:43

4 Answers4

22

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.

Gawil
  • 1,171
  • 6
  • 13
  • My mistake, it works, thanks! Why trailing '\s?' is needed? – dokondr May 17 '17 at 15:01
  • 1
    @dokondr: It's just that if you use only `\S*@\S*`, your remaining words will be separated by more than one space if an address has been deleted between them. By adding `\s?`, each time you delete an address, you will delete one space with it – Gawil May 17 '17 at 15:22
  • I have added explanations. Tell me if you do not understand something – Gawil May 17 '17 at 15:36
3

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 user@xxx.com 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.

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
2

Adding as nobody as added a regex :

text= 'abc user@xxx.com 123 any@www foo @ bar 78@ppp @5555 aa@111'

required_output=re.sub(r'[A-Za-z0-9]*@[A-Za-z]*\.?[A-Za-z0-9]*', "", text)
    
required_output=" ".join(required_output.split())
TBhavnani
  • 721
  • 7
  • 12
1
out = ' '.join([item for item in inp.split() if '@' not in item])
stamaimer
  • 6,227
  • 5
  • 34
  • 55