Skip to content

Instantly share code, notes, and snippets.

@aliaksei135
Created June 28, 2021 18:49
Show Gist options
  • Save aliaksei135/bcb20ecc2d2bffe55167d1cc5dc75a99 to your computer and use it in GitHub Desktop.
Save aliaksei135/bcb20ecc2d2bffe55167d1cc5dc75a99 to your computer and use it in GitHub Desktop.
Email Plus Addressing Cleaner
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <https://unlicense.org>
import regex # builtin `re` module doesn't like variable lookbehinds
from builtins import print
def clean_email_plus_addressing(email):
plus_matcher = regex.compile('(?<=.+?)\\+.*(?=\\@.+=?\\.=?)')
cleaned_email = regex.sub(plus_matcher, '', email)
return cleaned_email
if __name__ == '__main__':
# https://regexr.com/60sng
assert clean_email_plus_addressing('asdf1234+ss@email.au') == 'asdf1234@email.au'
assert clean_email_plus_addressing('lkjh@email.co.tz') == 'lkjh@email.co.tz'
assert clean_email_plus_addressing('123qwert123+123zxcv123@email123.com') == '123qwert123@email123.com'
assert clean_email_plus_addressing('zxcv123++123+asdf+123++@email++.co.uk') == 'zxcv123@email++.co.uk'
print('Asserts all pass!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment