正则表达式从电子邮件地址中提取顶级域名 [英] Regex to extract top level domain from email address
本文介绍了正则表达式从电子邮件地址中提取顶级域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通过电子邮件地址
xxx@site.co.uk
xxx@site.uk
xxx@site.me.uk
在所有情况下,我想编写一个应返回"uk"的正则表达式.
I want to write a regex which should return 'uk' is all the cases.
我尝试过
'+@([^.]+)\..+'
仅给出域名.我尝试使用
which gives only the domain name. I have tried using
'[^/.]+$'
但是它给出了错误.
推荐答案
提取所需内容的正则表达式为:
The regex to extract what you are asking for is:
\.([^.\n\s]*)$ with /gm modifiers
说明:
\. 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
输出:
['uk', 'uk', 'uk']
希望这会有所帮助.
这篇关于正则表达式从电子邮件地址中提取顶级域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文