简体   繁体   中英

re.search multiple patterns

I am parsing the script line by line to find the following pattern "print("xxxx") or "LOG.info("xxxx").

  1. When the below pattern is executed, it matches all the line starts with print SeacrhOutput = re.search(r'print\(((.*?)\))',line)
  2. however, following error was observed - 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)

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 ) .

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
--------------------------------------------------------------------------------
  \)                       ')'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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