繁体   English   中英

重新研究多种模式

[英]re.search multiple patterns

我正在逐行解析脚本以找到以下模式“print(“xxxx”)或“LOG.info(“xxxx”)。

  1. 当执行下面的模式时,它匹配所有以 print SeacrhOutput = re.search(r'print\(((.*?)\))',line)
  2. 但是,观察到以下错误 - sre_constants.error: bad escape \L at position 5当 LOG.info 添加到模式re.search(r'print\|LOG.info\(((.*?)\))',line)

利用

(?:print|LOG\.info)\((.*?)\)

证明 该表达式将匹配printLOG.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.

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