[Tutor] How to find all emails (on the same line)

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri, 21 Jun 2002 12:46:23 -0700 (PDT)


On Fri, 21 Jun 2002, A wrote:

> I need to find ALL emails in a file. I can use re module but I do not
> know how to find all emails if there are more emails on the same
> line because re module ( as far as I know )finds only one/first
> occurrence on that line.

Ah!  Try the findall() method from the 're' module:

###
>>> def findAllNumbers(line):
...     return re.findall(r'\d+', line)
...
>>> findAllNumbers("this is a test 42")
['42']
>>> findAllNumbers("this is a test 42 but 43 and 44 are also nice")
['42', '43', '44']
###


Hope this helps!