为什么`OneOrMore`在pyparsing中失败了多个表达式?

时间:2016-12-07 06:53:53

标签: python grammar text-parsing pyparsing

好的,我整理了一个解析器来解析像

这样的表达式
abc def ghi LONG|SHORT 12345

----^------ ----^----- --^--
 A: alphas  B: choice  C: num

pyparsing编写的解析器如下所示:

a = pp.OneOrMore(pp.Word(pp.alphas)).setName("PRT_A")
b = pp.Or(['LONG','SHORT']).setName("PRT_B")
c = pp.Word(pp.nums).setName("PRT_C")
expr = a('A') + b('B') + c('C')

当我喂食时,有些东西说短片13"进入它:

res = expr.parseString("something said SHORT 13")

我收到错误:

ParseException: 
Expected {"LONG" ^ "SHORT"} (at char 21), (line:1, col:22)
"something said SHORT >!<13"

为什么呢?我认为OneOrMore应该加上所有的单词,直到LONG | SHORT选择来......

1 个答案:

答案 0 :(得分:2)

此处的问题是<section id="intro" class="image-slider"> <div class="container" id="intro-slide"> <div class="slide-box"> <img src="http://www.jkoffset.com/assets/images/Packaging1.jpg" alt="same-box-slide" width="150px"> <div class="slide-txt"> <h1 class="title">Headline <span>Goes Here</span></h1> <div class="caption"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> <a class="btn" href="#"> Learn More </a> </div> </div> </div> </div> </section>同时包含pp.Word(pp.alphas)"LONG",阻止"SHORT"匹配。您需要阻止它与这些关键字匹配,例如使用否定前瞻:

b

或使用b = pp.Or(['LONG','SHORT']).setName("PRT_B") a = pp.OneOrMore(~b + pp.Word(pp.alphas)).setName("PRT_A") 的{​​{1}}选项:

stopOn