Search code examples
regexpython-re

Regex remove certain part of an email


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+sometext@gmail.com john+som_et.ext@gmail.com and so on..

I would like remove the +1, +sometext, +som_et.ext so I'm always left john@gmail.com

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

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+asdf@gmail.com')





Solution

  • 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.