[import re] match or findall?

Discussion in 'Python' started by gohaku, Jul 3, 2004.

  1. gohaku

    gohaku Guest

    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
    gohaku, Jul 3, 2004
    #1
    1. Advertising

  2. gohaku

    Sam Holden Guest

    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
    Sam Holden, Jul 4, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWc=?=

    Active Directory problem - search.FindAll()

    =?Utf-8?B?bWc=?=, Feb 22, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    2,365
    =?Utf-8?B?bWc=?=
    Feb 22, 2004
  2. =?Utf-8?B?QW5kcmV3MTI0?=

    DirectorySearcher.FindAll()

    =?Utf-8?B?QW5kcmV3MTI0?=, Jul 11, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    2,300
    =?Utf-8?B?QW5kcmV3MTI0?=
    Jul 11, 2004
  3. Ezequiel, Justin

    optional arguments to pattern.findall()

    Ezequiel, Justin, May 5, 2004, in forum: Python
    Replies:
    1
    Views:
    331
    Peter Otten
    May 5, 2004
  4. Will Stuyvesant

    re.findall

    Will Stuyvesant, Jul 16, 2004, in forum: Python
    Replies:
    1
    Views:
    530
    Matthias Huening
    Jul 16, 2004
  5. JerryB

    re.match versus re.findall

    JerryB, Feb 9, 2006, in forum: Python
    Replies:
    1
    Views:
    364
    Peter Hansen
    Feb 10, 2006
Loading...

Share This Page