-2

I want to extract top level domains from email addresses with Python. I want to use a regular expression to get everything after the FIRST dot after the @ symbol

E.g.

tony.manero@testing.com
ted.br@top.domain.us

Expected result

.com
.domain.us

How can I use regex to achieve this result?

Alex
  • 1,447
  • 7
  • 23
  • 48

1 Answers1

2

You can try this regex and capture group 1 content,

@.*?\.(.*)

Demo

Explanation:

  • @.*?. --> Matches @ character followed by any characters in non-greedy way followed by a literal dot
  • (.*) --> Capture whatever remains after that which is top level domain name

Here is a sample python code,

import re
s = ['tony.manero@testing.com','ted.br@top.domain.us']
for a in s:
    print (a,' --> ',re.findall('@.*?\.(.*)',a))

This prints following output,

tony.manero@testing.com  -->  ['com']
ted.br@top.domain.us  -->  ['domain.us']
Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36