py解析错误
我在 pyparsing 中遇到此错误
from pyparsing import Word,alphas,nums,Or,Regex,StringEnd
ws = Regex('\s*')
dot = "."
w = Word(alphas) + (ws | dot) + StringEnd()
w.leaveWhitespace()
w.parseString('AMIT.')
返回以下错误:
ParseException: Expected end of text (at char 4), (line:1, col:5)
I am stuck at this error in pyparsing
from pyparsing import Word,alphas,nums,Or,Regex,StringEnd
ws = Regex('\s*')
dot = "."
w = Word(alphas) + (ws | dot) + StringEnd()
w.leaveWhitespace()
w.parseString('AMIT.')
Returns the following error:
ParseException: Expected end of text (at char 4), (line:1, col:5)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
|
创建“首先匹配”表达式,而不是“最长匹配”。第一个替代方案是正则表达式,它将匹配 0 个或多个空白字符。事实上,这确实匹配,因此不会解析该点。
那么下一个要解析的元素是
StringEnd
,但解析位置仍然位于“.”处——所以,失败!以下是通过向语法表达式添加
setDebug()
调用而获得的一些更详细的输出:要使语法正常工作,您可以:
将
|
运算符更改为^
(匹配最长而不是首先匹配)更改正则表达式为
\s+
而不是\s*
(以便匹配至少需要一个空格)将第二个术语更改为
可选(点)
一般来说,对空白的显式测试与pyparsing 哲学——pyparsing 与 re 不同。
|
creates a "match first" expression, not "match longest".The first alternative is the regex, which will match 0 or more whitespace characters. This, in fact, does match, so the dot is not parsed.
Then the next element to parse is
StringEnd
, but the parse position is still located at the '.'—so, fail!Here is some more detailed output by adding
setDebug()
calls to your grammar expressions:To get your grammar to work you could:
change the
|
operator to^
(match longest instead of match first)change the regex to
\s+
instead of\s*
(so that at least one space was required for a match)change your second term to
Optional(dot)
In general, explicit testing for whitespace is not consistent with the pyparsing philosophy—pyparsing is not the same as re.