2014-04-01 47 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' 
相關問題