在python正则表达式中,如果匹配任何一个正则表达式值,我将如何匹配大量文本和标志...我已尝试使用"|" 或者我已经尝试制作一个正则表达式列表..既没有为我工作..这里有一个例子,我正在尝试用或...
我认为我的"或"被评论出来了
patterns=re.compile(r'[\btext String1\b] | [\bText String2\b]')
if(patterns.search(MyTextFile)):
print ("YAY one of your text patterns is in this file")
Run Code Online (Sandbox Code Playgroud)
上面的代码总是说它匹配,无论字符串是否出现,如果我改变它,我在第一个正则表达式上匹配,但从不检查第二个....我相信这是因为"Raw"正在评论我的或声明,但我怎么能解决这个问题?
我也尝试通过取出"Raw"语句并在我的\ b上放置双斜线来逃避,但这也不起作用:(
patterns=re.compile(\\btext String1\\b | \\bText String2\\b)
if(patterns.search(MyTextFile)):
print ("YAY one of your text patterns is in this file")
Run Code Online (Sandbox Code Playgroud)
然后我尝试用or做两个单独的原始语句,并且解释器抱怨不支持的str opperands ...
patterns=re.compile(r'\btext String1\b' | r'\bText String2\b')
if(patterns.search(MyTextFile)):
print ("YAY one of your text patterns is in this file")
Run Code Online (Sandbox Code Playgroud)