jack west
jack west

Reputation: 641

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 [email protected] [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 [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('[email protected]')




Upvotes: 1

Views: 75

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627517

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.

Upvotes: 1

Related Questions