Regex to extract top level domain from email address

孤者浪人 提交于 2019-12-04 06:15:10

问题


From email address like

xxx@site.co.uk
xxx@site.uk
xxx@site.me.uk

I want to write a regex which should return 'uk' is all the cases.

I have tried

'+@([^.]+)\..+' 

which gives only the domain name. I have tried using

'[^/.]+$'  

but it is giving error.


回答1:


The regex to extract what you are asking for is:

\.([^.\n\s]*)$  with /gm modifiers

explanation:

    \. matches the character . literally
1st Capturing group ([^.\n\s]*)
    [^.\n\s]* match a single character not present in the list below
        Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
        . the literal character .
        \n matches a fine-feed (newline) character (ASCII 10)
        \s match any white space character [\r\n\t\f ]
$ assert position at end of a line
m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
g modifier: global. All matches 

for your input example, it will be:

import re
m = re.compile(r'\.([^.\n\s]*)$', re.M)                                             
f = re.findall(m, data)                                                             
print f 

output:

['uk', 'uk', 'uk']

hope this helps.




回答2:


As myemail@com is a valid address, you can use:

@.*([^.]+)$



回答3:


You don't need regex. This would always give you 'uk' in your examples:

>>> url = 'foo@site.co.uk'
>>> url.split('.')[-1]
'uk'



回答4:


Simply .*\.(\w+) won't help?

Can add more validations for "@" to the regular expression if needed.



来源:https://stackoverflow.com/questions/22785243/regex-to-extract-top-level-domain-from-email-address

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!