简体   繁体   English

Python re.search regex using or 条件

[英]Python re.search regex using or condition

Hi Friends I'm trying to include "or |"嗨,朋友们,我正在尝试包含“或 |” condition in search pattern using re.search.使用 re.search 搜索模式中的条件。 Can someone help me how to achieve or condition as I'm not getting match.有人可以帮助我如何实现或条件,因为我没有得到匹配。 Below code works下面的代码有效

>>> 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")
...
>>>

I want to match above string "apple."我想匹配上面的字符串“apple”。 followed by fruit[0] or vegi[0]接着是fruit[0] 或vegi[0]

Regex or should be achieved through |正则表达式还是应该通过|来实现的operator and we don't inculde this inside re.escape .运算符,我们不会在re.escape If you do so, then it would loose it's special meaning.如果你这样做,那么它就会失去它的特殊意义。

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

or或者

pattern = r'apple\.fruit\[0\]|vegi\[0\]'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM