Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [import re] match or findall?

Reply
Thread Tools

[import re] match or findall?

 
 
gohaku
Guest
Posts: n/a
 
      07-03-2004
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


 
Reply With Quote
 
 
 
 
Sam Holden
Guest
Posts: n/a
 
      07-03-2004
On Sat, 3 Jul 2004 18:02:45 -0400, gohaku <> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
re.sub(): replace longest match instead of leftmost match? John Gordon Python 13 12-20-2011 02:58 AM
pat-match.lisp or extend-match.lisp in Python? ekzept Python 0 08-10-2007 06:08 PM
Match doesn't match Volkan Civelek Ruby 4 07-19-2006 07:44 AM
$match = true() for empty $match?? Victor XML 2 05-17-2004 10:43 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57