[英]re.search multiple patterns
I am parsing the script line by line to find the following pattern "print("xxxx") or "LOG.info("xxxx").我正在逐行解析脚本以找到以下模式“print(“xxxx”)或“LOG.info(“xxxx”)。
SeacrhOutput = re.search(r'print\(((.*?)\))',line)
当执行下面的模式时,它匹配所有以 print SeacrhOutput = re.search(r'print\(((.*?)\))',line)
sre_constants.error: bad escape \L at position 5
when the LOG.info was added to the pattern re.search(r'print\|LOG.info\(((.*?)\))',line)
但是,观察到以下错误 - sre_constants.error: bad escape \L at position 5
当 LOG.info 添加到模式re.search(r'print\|LOG.info\(((.*?)\))',line)
Use利用
(?:print|LOG\.info)\((.*?)\)
See proof .见证明。 The expression will match print
or LOG.info
with (
after them, and then capture any zero or more characters into a group up to the leftmost )
.该表达式将匹配print
或LOG.info
与(
在它们之后,然后将任何零个或多个字符捕获到最左边的组中)
。
EXPLANATION:解释:
NODE EXPLANATION
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
print 'print'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
LOG 'LOG'
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
info 'info'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
\( '('
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\) ')'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.