像pyparsing中的optionalQuotedString一样?

时间:2012-03-16 22:10:20

标签: python parsing pyparsing

在我的解析器中,我多次出现

expression = quotedString(pattern) | Word(pattern)

我想知道是否有一些我错过的内置课程,或者我是否已经自己定义了。在第二种情况下,最好的选择是什么?

2 个答案:

答案 0 :(得分:2)

你的意思是这样的:

def quotedStringOrWord(pattern):
    return quotedString(pattern) | Word(pattern)

答案 1 :(得分:1)

我知道旧线程,但你可以在其他表达式中重用你的表达式。

optionalQuotedString = QuotedString(pattern) | Word(pattern)

expression1 = optionalQuotedString
expression2 = Literal('>') + optionalQuotedString

可能比功能更整洁,但做同样的事情。