[英]re.search multiple patterns
我正在逐行解析腳本以找到以下模式“print(“xxxx”)或“LOG.info(“xxxx”)。
SeacrhOutput = re.search(r'print\(((.*?)\))',line)
sre_constants.error: bad escape \L at position 5
當 LOG.info 添加到模式re.search(r'print\|LOG.info\(((.*?)\))',line)
利用
(?:print|LOG\.info)\((.*?)\)
見證明。 該表達式將匹配print
或LOG.info
與(
在它們之后,然后將任何零個或多個字符捕獲到最左邊的組中)
。
解釋:
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.