Search code examples
pythonpyparsing

How to get the whole line containing several key words by pyparsing?


I am using pyparsing to parse the log files. I want to get the whole line which contains the keywords:

  • Input/Output error
  • Disk full
  • Quota Exceeded

My code is below:

import pyparsing as pp
line = pp.Combine(pp.Regex(".*") + pp.CaselessLiteral("Input/Output error") ^ \
       pp.CaselessLiteral("Disk full") ^ pp.CaselessLiteral("Quota Exceed") + \ 
       pp.Regex(".*"))

In the match result , it just contains the keyword such as "Disk full". It doesn't get the whole line.

Does anyone know how to get the whole line in the result?


Solution

  • Try attaching this parse action to line:

    def return_containing_line(s, l, t):
        # use pyparsing line builtin to extract the current line text
        # at location 'l' in input string 's'
        return pp.line(l, s)
    line.addParseAction(return_containing_line)
    

    Is this parser actually working? I would think that the leading Regex would consume the entire line, since it does not do any lookahead to the following literal expressions. Why not drop the Regex terms and just use searchString? Or if you use scanString, you will get a (tokens, startloc, endloc) tuple for each match, and you can just call pp.line using the startloc and the input string, and you wouldn't even need the parse action.