[import re] match or findall?

Sam Holden sholden at flexal.cs.usyd.edu.au
Sun Jul 4 01:08:51 CEST 2004


On Sat, 3 Jul 2004 18:02:45 -0400, gohaku <gohaku at earthlink.net> wrote:
> Hi everyone,
> I am having a problem with the match function:
> 
> import re
> string = 'abc 123456 xyz'
> if re.match("\d{1,}",string):
>          print "Found a number" #Does not print
> 
> 
> whereas findall works:
> 
> import re
> string = 'abc 123456 xyz'
> if re.findall("\d{1,}",string):
>          print "Found a number" #Actually prints
> 
> Is there a function similar to findall that will find the 1st 
> occurrence?
> I realize match is probably that similar function but I can't get this 
> simple
> example working, for some reason.

Match only matches at the beginning of the string, which is very different from
finding the first occurance.


match = re.search("\d{1,}",string)
if match is not None:
	print "Found a number:", match.group()

-- 
Sam Holden



More information about the Python-list mailing list