在 if 条件中使用 re.search 匹配多个字符串

Match multiple strings using re.search within an if condition

我正在编写一个程序,根据另一个单元格中的 position 值从电子表格创建列表。所以我的代码看起来像

for j in xrange(1,13):
    for sheet in wb.sheets():
        for i in xrange(1,12*15):
            team=sheet.cell(i,0)
            position=sheet.cell(i,2)
            games=sheet.cell(i,23)
            if re.match(owner[j], str(team.value)) and (not re.findall('Defense' or 'K,' or 'KFG' or 'KKO', str(position.value))):
                try:
                    list.append(int(games.value))
                except ValueError:
                    list.append(0)
            else:
                pass
    print list
    list=[]

所以这样做的目的是当第一列中的行匹配 owner 时追加到列表中,而不是 Defense K, KFG KKO在位置栏中。

不幸的是,KKFGKKO 的值都显示 在我的列表中,但 Defense 值不正确。我怎样才能 确保满足其他筛选条件?

作为旁注,这些位置在其他文本中,因此 这里使用 search() 而不是 match().

"Defense" 是一个 'truthy' 值,所以结果:

'Defense' or 'K,' or 'KFG' or 'KKO'

'Defense'

因此,您的情况无异于:

re.match(owner[j], str(team.value)) and (not re.findall('Defense', str(position.value)))

如果您想要正则表达式中的替代项,请在模式中使用 |

re.match(owner[j], str(team.value)) and (not re.findall('Defense|K,|KFG|KKO', str(position.value)))