Home > Back-end >  Regex remove certain part of an email
Regex remove certain part of an email

Time:09-05

I'm trying to remove certain parts of an email using regex in python

For instance john@gmail can also create john 1@gmail john [email protected] john [email protected] and so on..

I would like remove the 1, sometext, som_et.ext so I'm always left [email protected]

If I use ([ ])\w (https://regexr.com/6t7ls) this fails if the email is something like john [email protected]

I'm also not totally sure how to convert this to python I have tried the following which does not work

REMOVE_PLUS_ADDRESSES = re.compile('([ ])\w /g')

m = REMOVE_PLUS_ADDRESSES.match('asdfasd [email protected]')




CodePudding user response:

You can use

re.sub(r'\ [^\s@]*(?=@)', '', text)

See the regex demo.

Details:

  • \ - the char
  • [^\s@]* - zero or more chars other than whitespace and @
  • (?=@) - a location immediately before a @ char.
  • Related