2014-04-01 51 views
0

顶级域名从电子邮件地址一样正则表达式来提取电子邮件地址

[email protected] 
[email protected] 
[email protected] 

我想写一个正则表达式应该返回“英国”是所有的案件。

我已经试过

'[email protected]([^.]+)\..+' 

其中只给出域名。我曾尝试使用

'[^/.]+$' 

但它给错误。

+0

你最后一个错误是什么?你能展示你正在使用的实际代码吗? – Jerry

+0

如何简单地使用'。+ @。+(\。[\ w +])'? –

+0

是否需要使用正则表达式? 'email_address.rsplit(“。”,1)[1]'? – Blckknght

回答

2

正则表达式来提取你问的是:

\.([^.\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'] 

希望这有助于。

-1

简单.*\.(\w+)不会帮助?

如果需要,可以将“@”的更多验证添加到正则表达式中。

1

你不需要正则表达式。这会在你的例子中总是给你'英国':

>>> url = '[email protected]' 
>>> url.split('.')[-1] 
'uk' 
相关问题