[英]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.我们在某些解析器中使用
to_eol = SkipTo(lineEnd(), include=True)
来吃掉该行的 rest。 Since SkipTo
re-evaluates the inner parser at every position, I was trying to simplify the work using Regex
.由于
SkipTo
在每个 position 重新评估内部解析器,我试图使用Regex
简化工作。
As lineEnd
only recognizes \n
, I thought that Regex('[^\\n]*\\n')
should work.由于
lineEnd
只识别\n
,我认为Regex('[^\\n]*\\n')
应该可以工作。
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.
我尝试了一堆变体 - 添加多行正则表达式模式,连续接受多个换行符等,但它们都没有奏效。
You may be tripping over pyparsing's default whitespace skipping and regex multiline.您可能会绊倒 pyparsing 的默认空格跳过和正则表达式多行。 Pyparsing defines a helper
restOfLine
as Regex(r".*").leaveWhitespace().setName("rest of line")
. Pyparsing 将帮助
restOfLine
定义为Regex(r".*").leaveWhitespace().setName("rest of line")
。 Note that this might match an empty string, so OneOrMore(restOfLine)
will loop forever.请注意,这可能匹配一个空字符串,因此
OneOrMore(restOfLine)
将永远循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.