Search code examples
pythonpyparsing

pyparsing optimize skip rest of line


We use to_eol = SkipTo(lineEnd(), include=True) in some of our parsers to eat the rest of the line. Since SkipTo re-evaluates the inner parser at every position, I was trying to simplify the work using Regex.

As lineEnd only recognizes \n, I thought that Regex('[^\\n]*\\n') should work.

It doesn't, and I can't quite figure out why. I've tried a bunch of variants - adding multiline regex mode, accepting more than one newline in a row, etc., but none of them have worked.


Solution

  • You may be tripping over pyparsing's default whitespace skipping and regex multiline. Pyparsing defines a helper restOfLine as Regex(r".*").leaveWhitespace().setName("rest of line"). Note that this might match an empty string, so OneOrMore(restOfLine) will loop forever.