頂級域名從電子郵件地址一樣正則表達式來提取電子郵件地址
[email protected]
[email protected]
[email protected]
我想寫一個正則表達式應該返回「英國」是所有的案件。
我已經試過
'[email protected]([^.]+)\..+'
其中只給出域名。我曾嘗試使用
'[^/.]+$'
但它給錯誤。
頂級域名從電子郵件地址一樣正則表達式來提取電子郵件地址
[email protected]
[email protected]
[email protected]
我想寫一個正則表達式應該返回「英國」是所有的案件。
我已經試過
'[email protected]([^.]+)\..+'
其中只給出域名。我曾嘗試使用
'[^/.]+$'
但它給錯誤。
正則表達式來提取你問的是:
\.([^.\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']
希望這有助於。
由於[email protected]
是一個有效的地址,你可以使用:
@.*([^.]+)$
簡單.*\.(\w+)
不會幫助?
如果需要,可以將「@」的更多驗證添加到正則表達式中。
你不需要正則表達式。這會在你的例子中總是給你'英國':
>>> url = '[email protected]'
>>> url.split('.')[-1]
'uk'
你最後一個錯誤是什麼?你能展示你正在使用的實際代碼嗎? – Jerry
如何簡單地使用'。+ @。+(\。[\ w +])'? –
是否需要使用正則表達式? 'email_address.rsplit(「。」,1)[1]'? – Blckknght