Python:列表和字符串匹配

pet*_*ete 8 python match

我有以下内容:

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)
Run Code Online (Sandbox Code Playgroud)

re.match只匹配字符串的开头,如何在中间匹配子字符串.

Mar*_*ers 14

你可以用re.search而不是re.match.

在这里你似乎并不需要正则表达式.你的正则表达式123.35可能没有达到预期的效果,因为点匹配任何东西.

如果是这种情况,那么您可以使用简单的字符串包含x in s.


YOU*_*YOU 12

使用re.search或仅使用if l in temp:

注意:内置类型list不应该被遮蔽,因此for l in lists:更好