简体   繁体   中英

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')




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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM