Search code examples
python-3.xstringsplitdata-cleaning

How to split string of emails with different domains and no separator


How can one separate a string of emails with no separator and different domains/number of letters after ‘@‘?

[email protected]@[email protected]@us.org

Is it possible to structure: If ends with .br or .se: separate there Else, separate after .com ?


Solution

  • One possibility is to use regular expression (regex101):

    import re
    
    s = "[email protected]@[email protected]@us.org"
    
    emails = re.findall(r"[^@]+@[^@]+(?:\.com|\.se|\.br|\.org)", s)
    print(emails)
    

    Prints:

    ['[email protected]', '[email protected]', '[email protected]', '[email protected]']