pete pete - 2 months ago 11
Python Question

Python: list and string matching

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.

Answer

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.