Puzzled about this regex

J

Jean-Claude Neveu

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

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top