Puzzled about this regex

Discussion in 'Python' started by Jean-Claude Neveu, Apr 18, 2009.

  1. Hello,

    I wonder if someone could tell me where I am going wrong with my
    regular expression, please. My regex only matches the text I'm
    looking for (a number followed by a distance unit) when it appears at
    the beginning of the string. But I am not using the ^ character
    (which would indicate that I only want a match if it is at the start).

    -------------------------------
    #
    import re

    regex1 = re.compile("[0-9]+ (feet|meters)", re.IGNORECASE)

    def is_distance(str):
    if regex1.match(str):
    print "distance"
    else:
    print "not distance"

    # First one matches, second does not -- WHY?
    is_distance("300 feet is the distance")
    is_distance("The distance is 300 feet")
    -------------------------------

    Thank you!

    J-C
    Jean-Claude Neveu, Apr 18, 2009
    #1
    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. Mark Andrew

    VHDL: puzzled beginner

    Mark Andrew, Jun 27, 2004, in forum: VHDL
    Replies:
    5
    Views:
    629
    Mark Andrew
    Jun 30, 2004
  2. Daniel

    still puzzled

    Daniel, Jun 8, 2004, in forum: ASP .Net
    Replies:
    11
    Views:
    642
    Steven Cheng[MSFT]
    Jun 9, 2004
  3. schiefaw

    Very Puzzled, Please Help

    schiefaw, Jul 22, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    368
    schiefaw
    Jul 22, 2005
  4. Replies:
    5
    Views:
    1,388
  5. Replies:
    3
    Views:
    726
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page