Python 正则表达式从电子邮件地址提取顶级域
从电子邮件地址Python 正则表达式从电子邮件地址提取顶级域,python,regex,tld,Python,Regex,Tld,从电子邮件地址 xxx@site.co.uk xxx@site.uk xxx@site.me.uk 我想写一个正则表达式,它应该返回'uk'是所有情况 我试过了 '+@([^.]+)\..+' 它只给出了域名。我试过使用 '[^/.]+$' 但是它给出了一个错误。提取您所要求的正则表达式是: \.([^.\n\s]*)$ with /gm modifiers 说明: \. matches the character . literally 1st Capturing gr
xxx@site.co.uk
xxx@site.uk
xxx@site.me.uk
我想写一个正则表达式,它应该返回'uk'是所有情况
我试过了
'+@([^.]+)\..+'
它只给出了域名。我试过使用
'[^/.]+$'
但是它给出了一个错误。提取您所要求的正则表达式是:
\.([^.\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
对于您的输入示例,它将是:
import re
m = re.compile(r'\.([^.\n\s]*)$', re.M)
f = re.findall(m, data)
print f
输出:
['uk', 'uk', 'uk']
希望这有帮助。提取您所需内容的正则表达式是:
\.([^.\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
对于您的输入示例,它将是:
import re
m = re.compile(r'\.([^.\n\s]*)$', re.M)
f = re.findall(m, data)
print f
输出:
['uk', 'uk', 'uk']
希望这有帮助。作为
myemail@com
是一个有效地址,您可以使用:
@.*([^.]+)$
As
myemail@com
是一个有效地址,您可以使用:
@.*([^.]+)$
简单地*\(\w+)
不会有帮助吗
如果需要,可以将“@”的更多验证添加到正则表达式中。简单地*\.(\w+)
不会有帮助吗
如果需要,可以将“@”的更多验证添加到正则表达式中。您不需要正则表达式。在您的示例中,这始终会给您带来“uk”:
>>> url = 'foo@site.co.uk'
>>> url.split('.')[-1]
'uk'
你不需要正则表达式。在您的示例中,这始终会给您带来“uk”:
>>> url = 'foo@site.co.uk'
>>> url.split('.')[-1]
'uk'
最后一个错误是什么?您能展示一下您正在使用的实际代码吗?简单地
++.++(\.[\w+])
?是否需要正则表达式?电子邮件地址.rsplit(“.”,1)[1]怎么样?这些字符串是分开的吗?然后您可以只执行\w+$
@amitbisai您的第二个正则表达式几乎是正确的。你刚才用错了转义符号。它应该是`
-[^\.]+$
。最后一个错误是什么?您能展示一下您正在使用的实际代码吗?简单地++.++(\.[\w+])
?是否需要正则表达式?电子邮件地址.rsplit(“.”,1)[1]怎么样?这些字符串是分开的吗?然后您可以只执行\w+$
@amitbisai您的第二个正则表达式几乎是正确的。你刚才用错了转义符号。它应该是`
-[^\.]+$
。