python和正则表达式

tek*_*agi 1 python regex


#!/usr/bin/python
import re

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

它不是最复杂的代码,我正在寻找一种获得所有匹配的方法,如果可能的话.

Joh*_*web 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.
Run Code Online (Sandbox Code Playgroud)

至于识别电子邮件地址的实际正则表达式...请参阅此问题.

另外,请注意使用str变量名称.这将隐藏str内置功能.