简体   繁体   English

在python中的re.search中一起使用AND&OR运算符?

[英]Using AND & OR operator together in re.search in python?

Is there a way to use AND & OR operator together in re.search in python?有没有办法在python中的re.search中一起使用AND&OR运算符?

if re.search(r'xyz|abc', line) != None:

Here I have used OR operator, But my requirement is to get line containing ( 'xyz' or 'abc' ) and 'pqr' .在这里,我使用了 OR 运算符,但我的要求是获取包含( 'xyz''abc' )和'pqr' How to do that?怎么做?

This works:这有效:

>>> import re
>>> re.search( r'(xyz|abc)pqr', 'abcpqr' )
<_sre.SRE_Match object at 0x7fa3055bdeb0>
>>> re.search( r'(xyz|abc)pqr', 'abcpq' )
>>> 

To ignore the order of the strings you can use要忽略您可以使用的字符串顺序

if re.search(r'(?=.*[pqr])(?=.*[xyz|abc])', line):
    print('match')

line = 'xyzpqr' # match
line = 'pqrxyz' # match

尝试这个:

re.search(r'((xyz|abc).*pqr)|(pqr.*(xyz|abc))', line)

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

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