Search code examples
subdomain

How to extract the domain in a string by using regex?


I got a string representing users data.

What is the proper regex to extract domain in this string?

I know that I have to find all strings with 2 characters matching the condition that it comes after the last "." after a "@".

However I still failed to implement it.


Solution

  • import re
    
    regex = r"@.+\.([a-z]{2}),"
    
    your_string = ("001,Francisca,Dr Jhonaci,jhonadr@abc.com,32yearsold,120.238.225.0\n"
        "002,Lavenda,Bocina,lavenboci@banck.ac.uk,50yearsold,121.186.221.182\n"
        "003,Laura,Eglington,elinton@python.co.jp,26yearsold,36.55.173.63\n"
        "004,Timo,Baum,timobaum@tennis.co.cn,22yearsold,121.121.110.10")
    
    matches = re.finditer(regex, your_string, re.MULTILINE)
    
    for match in matches:
        result = match.group(1)
        print(result)