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

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Jun 21 21:46:23 CEST 2002


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!







More information about the Python-list mailing list