tags:

views:

119

answers:

2

Hi All,

I have following:

temp = "aaaab123xyz@+"

lists = ["abc", "123.35", "xyz", "AND+"]

for list in lists
  if re.match(list, temp, re.I):
    print "The %s is within %s." % (list,temp)

The re.match is only match the beginning of the string, How to I match substring in between too.

+6  A: 

You can use re.search instead of re.match.

It also seems like you don't really need regular expressions here. Your regular expression 123.35 probably doesn't do what you expect because the dot matches anything.

If this is the case then you can do simple string containment using x in s.

Mark Byers
yeah, yours is 5 seconds faster actually, +1
S.Mark
+6  A: 

Use re.search or just use in if l in temp:

Note: built-in type list should not be shadowed, so for l in lists: is better

S.Mark
Heh, exactly the same answer, and just five seconds difference.
Mark Byers