[import re] match or findall?

G

gohaku

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.

anyone know what's wrong?

Thanks in advance.
-gohaku
 
S

Sam Holden

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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top