2011-01-13 103 views
1
 

#!/usr/bin/python 
import re 

str = raw_input("String containing email...\t") 
match = re.search(r'[\w.-][email protected][\w.-]+', str) 
if match: 
    print match.group() 

它不是最複雜的代碼,我正在尋找一種方法來獲得所有的匹配,如果可能的話。蟒蛇和正則表達式

+0

我不知道python,但是在perl中獲得所有匹配的修飾符是g,所以也許r'[\ w .-] + @ [\ w .-] +'g會起作用。 – 2011-01-13 06:26:01

+0

我相信你已經意識到這一點,但以防萬一:正則表達式錯過了一些有效的電子郵件地址,並會發現一些無效的地址。 – 2011-01-13 06:27:46

+0

我該如何解決它,@Mark – tekknolagi 2011-01-13 06:28:46

回答

5

這聽起來像你想re.findall()

findall(pattern, string, flags=0) 
    Return a list of all non-overlapping matches in the string. 

    If one or more groups are present in the pattern, return a 
    list of groups; this will be a list of tuples if the pattern 
    has more than one group. 

    Empty matches are included in the result. 

至於識別電子郵件地址的實際正則表達式去...查看this question

另外,請小心使用str作爲變量名稱。這將隱藏內置的str

0

你應該給find的一個嘗試()或的findAll()

的findall()一 模式所有出現,不只是第一個爲 搜索匹配的()一樣。例如,如果一個是 一個作家,想找到所有的 副詞的一些文字,他或她可能 使用的findAll()

http://docs.python.org/library/re.html#finding-all-adverbs

0
  • 你不使用的raw_input以你使用的方式。只需使用raw_input從控制檯獲取輸入即可。
  • 不要重寫內置的如str。使用有意義的名稱併爲其分配一個完整的字符串值。

  • 另外這是一個很好的想法,編譯你的模式很多次,它有一個正則表達式對象來匹配字符串。 (代碼所示)

我才意識到,一個完整的正則表達式來正是因爲每RFC822 could be a pageful匹配的電子郵件ID,否則這個片段應該是有用的。

import re 

inputstr = "[email protected], [email protected], [email protected], etc etc\t" 
mailsrch = re.compile(r'[\w\-][\w\-\.][email protected][\w\-][\w\-\.]+[a-zA-Z]{1,4}') 
matches = mailsrch.findall(inputstr) 
print matches