簡體   English   中英

email Python 的非貪婪正則表達式模式

[英]Non greedy regex pattern for email Python

我是正則表達式的新手,並且陷入了這種情況,我當前的正則表達式模式返回兩個結果,其中一個以電話號碼開頭。 (字符串在電話號碼和電子郵件之間沒有空格)。 我嘗試使用? 使表達式不貪婪,但不返回任何內容。

代碼
pattern = re.compile(r'\b[A-Za-z0-9._%.?-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b')

matches = pattern.finditer(Clean)

for match in matches:
   print(match.group(0))
Output
9012345678testemail.grp@gmail.com
support@testtest.com

我想你正在尋找的是:

pattern = re.compile(r"[a-zA-Z]+[\w.-]*@[a-z]+[.][a-z]+")

[a-zA-Z] -> email 以字母開頭

\w -> 任何字母、數字或下划線

[\w.-]* -> 0 個或多個字母、數字、下划線、. 或 -

[az]+ -> 1 個或多個字符長的小寫域

[.] -> 把. 在方括號中,因為。 在正則表達式中有特殊含義

pattern = re.compile(r"[a-zA-Z]+[\w.-]*@[a-z]+[.][a-z]+")
matches = pattern.findall("aar@g.com ooo11..com hellow_world@hello.world 1234hey@hey.com")
print(matches)

結果:

['aar@g.com', 'hellow_world@hello.world', 'hey@hey.com']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM