Python使用or条件重新搜索正则表达式

Python使用or条件重新搜索正则表达式,python,regex,Python,Regex,嗨,朋友们,我正在尝试使用re.search在搜索模式中包含“或|”条件。有人能帮我在我还没有拿到比赛的情况下,如何达到或保持状态吗。 下面的代码有效 >>> pattern = re.escape('apple.fruit[0]') >>> sig = 'apple.fruit[0]' >>> if re.search(pattern, sig): ... print("matched") ... matched >>&

嗨,朋友们,我正在尝试使用re.search在搜索模式中包含“或|”条件。有人能帮我在我还没有拿到比赛的情况下,如何达到或保持状态吗。 下面的代码有效

>>> pattern = re.escape('apple.fruit[0]')
>>> sig = 'apple.fruit[0]'
>>> if re.search(pattern, sig):
...    print("matched")
...
matched 
>>> pattern = re.escape('apple.fruit[0] or vegi[0]')
>>> if re.search(pattern, sig):
...    print("matched")
...
>>>

我想匹配上面的字符串“apple.”,后面跟水果[0]或蔬菜[0]

正则表达式,或者应该通过
|
操作符实现,我们不在
中包含此内容。如果你这样做,那么它就会失去它的特殊意义

pattern = re.escape('apple.fruit[0]')+ '|' + re.escape('vegi[0]')


我相信正则表达式中的
是由
|
表示的,而不是
。。。
pattern = r'apple\.fruit\[0\]|vegi\[0\]'