pyparsing:解析其中包含特殊单词的句子

pyparsing: parse a sentence with special words in it

我正在尝试用 pyparsing 编写一个程序来解析所有包含特殊单词的字符串。我写了下面的代码,但它不起作用:

from pyparsing import *
word = Word(alphas) 
sentence = OneOrMore(word)
day = Literal("day")
sentence_end_with_happy = sentence + day + sentence 
ret = sentence_end_with_happy.parseString("hi this is a nice day and everything is ok")

我试图解析一个带有特殊词"day"的句子,但在解析时出现错误...

pyparsing.ParseException: Expected "day" (at char 42), (line:1, col:43)

pyparsing 正在抛出异常,因为它正在将 "day" 视为句子中的单词。

在这种情况下,您可以使用 python 内置模块 string 函数。

In [85]: str1 = "hi this is a nice day and everything is ok"
In [86]: str2 = "day"
In [87]: str2_pos = str1.find(str2)
In [88]: str1_split_str2 = [mystr[:str2_pos], mystr[str2_pos:str2_pos+len(str2)], mystr[str2_pos+len(str2):]]

In [89]: str1_split_str2
Out[89]: ['hi this is a nice ', 'day', ' and everything is ok']

定义word时使用负先行;否则,word 匹配 day 并且 sentence 将使用它。

from pyparsing import *
day = Keyword("day")
word = ~day + Word(alphas)
sentence = OneOrMore(word)
sentence_end_with_happy = sentence('first') + day + sentence('last') 
ret = sentence_end_with_happy.parseString("hi this is a nice day and everything is ok")
print ret['first']
print ret['last']
print ret

输出:

['hi', 'this', 'is', 'a', 'nice']
['and', 'everything', 'is', 'ok']
['hi', 'this', 'is', 'a', 'nice', 'day', 'and', 'everything', 'is', 'ok']