2017-08-04 74 views
-1

我將從更大的CSV文件收集分散的電子郵件。我正在學習正則表達式。我正試圖從這個例句中提取電子郵件。但是,電子郵件僅在@之前填寫了@符號和字母。你能幫我看看有什麼問題嗎?在Python中使用正則表達式捕獲電子郵件

import re 

String = "'Jessica's email is [email protected], and Daniel's email is [email protected] Edward's is [email protected], and his grandfather, Oscar's, is [email protected]'" 

emails = re.findall(r'.[@]', String) 
names = re.findall(r'[A-Z][a-z]*',String) 

print(emails) 
print(names) 
+0

如果重複不能幫助您,請查看您收到的答案並考慮接受其中的答案。 –

回答

1

您正則表達式的電子郵件是不工作:emails = re.findall(r'.[@]', String)比賽anychar然後@

我會嘗試不同的方法:匹配句子和具有以下經驗性假設提取的姓名,電子郵件夫婦(如果你的文本改變了太多,這樣會破壞邏輯)

  • 所有的名字都是其次's"is地方(使用非貪婪.*?以匹配所有的都
  • \w匹配任何alphanum字符之間,加下劃線來匹配多個地址&域,只有一個域(其它點它最後的點相匹配的句子)

代碼:

import re 

String = "'Jessica's email is [email protected], and Daniel's email is [email protected] Edward's is [email protected], and his grandfather, Oscar's, is [email protected]'" 

print(re.findall("(\w+)'s.*? is ([\w_][email protected][\w_]+\.[\w_]+)",String)) 

結果:

[('Jessica', '[email protected]'), ('Daniel', '[email protected]'), ('Edward', '[email protected]'), ('Oscar', '[email protected]')] 

轉換爲dict甚至會給你一個字典名=>地址:

{'Oscar': '[email protected]', 'Jessica': '[email protected]', 'Daniel': '[email protected]', 'Edward': '[email protected]'} 
0
  1. 電子郵件

    In [1382]: re.findall(r'\[email protected][\w_]+\.[\w_]+', text) 
    Out[1382]: 
    ['[email protected]', 
    '[email protected]', 
    '[email protected]', 
    '[email protected]'] 
    

它是如何工作的:所有的電子郵件是[email protected]。有一點要注意的是圍繞@的一堆字符,以及單數的.。因此,我們使用\S來劃分任何不是空白的東西。並且+是搜索1個或更多這樣的字符。 [\w_]+\.[\w_]+只是表示搜索字符串的一種奇特方式,其中只有一個.


  • 名稱

    In [1375]: re.findall('[A-Z][\S]+(?=\')', String) 
    Out[1375]: ['Jessica', 'Daniel', 'Edward', 'Oscar'] 
    
  • 工作原理:任意以大寫字。 (?=\')是一個向前看。如您所見,所有名稱均遵循Name's的模式。我們希望撇號之前的所有內容。因此,未被捕獲的前瞻。現在


    ,如果你想用一個巨大的正則表達式捕捉他們共同名稱映射到電子郵件,就可以了。 Jean-François Fabre's answer是一個好的開始。但我建議首先讓基礎知識下降。

    +0

    'odawg @ gmail.com.':最後的電子郵件最後有一個點,其他人也可以得到逗號:) –

    +0

    @ Jean-FrançoisFabre啊,shite。固定。謝謝。 –

    +0

    @ Jean-FrançoisFabre[過去24小時](https://puu.sh/x1aUC/32ef6e3aea.png)。是的,這是艱難的一天 - 強硬的人羣請。 –

    1

    你需要找到錨點,模式匹配。改進後的模式可能是:

    import re 
    
    String = "'Jessica's email is [email protected], and Daniel's email is 
    [email protected] Edward's is [email protected], and his 
    grandfather, Oscar's, is [email protected]'" 
    
    emails = re.findall(r'[a-zA-Z0-9_.+-][email protected][a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+', String) 
    names = re.findall(r'[A-Z][a-z]*', String) 
    
    print(emails) 
    print(names) 
    

    \ w +缺少' - ',這是電子郵件地址中允許的。

    0

    這是因爲您沒有使用重複操作符。下面的代碼使用+運算符,這意味着字符/子模式可以重複1次到多次。

    s = '''Jessica's email is [email protected], and Daniel's email is [email protected] Edward's is [email protected], and his grandfather, Oscar's, is [email protected]''' 
    
    p = r'[a-z0-9][email protected][a-z]+\.[a-z]+' 
    ans = re.findall(p, s) 
    
    print(ans) 
    
    +0

    謝謝m8。就像我說的,我是這個正則表達式的新手。 – EwokHugz

    相關問題