簡體   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