在pyparsing中区分匹配

Jak*_*ube 6 python parsing pyparsing python-2.7

我想用pyparsing解析一些单词和一些数字.简单吧.

from pyparsing import *

A = Word(nums).setResultsName('A')
B = Word(alphas).setResultsName('B')
expr = OneOrMore(A | B)

result = expr.parseString("123 abc 456 7 d")
print result
Run Code Online (Sandbox Code Playgroud)

上面的代码打印出来['123', 'abc', '456', '7', 'd'].一切顺利.现在我想用这些解析的值做一些工作.对于这项任务,我需要知道它们是否匹配AB.有没有办法区分这两者.

我在一些研究后发现的唯一的东西就是班级的items方法ParseResults.但它只返回[('A', '7'), ('B', 'd')],只有最后两场比赛.

我的计划/目标如下:

for elem in result:
    if elem.is_of_type('A'):
        # do stuff
    elif elem.is_of_type('B'):
        # do something else
Run Code Online (Sandbox Code Playgroud)

我如何区分AB

Pau*_*McG 7

getName()的工作很好.您还可以使用标记显式装饰返回的标记,指示所做的匹配:

def makeDecoratingParseAction(marker):
    def parse_action_impl(s,l,t):
        return (marker, t[0])
    return parse_action_impl

A = Word(nums).setParseAction(makeDecoratingParseAction("A"))
B = Word(alphas).setParseAction(makeDecoratingParseAction("B"))
expr = OneOrMore(A | B)

result = expr.parseString("123 abc 456 7 d")
print result.asList()
Run Code Online (Sandbox Code Playgroud)

得到:

[('A', '123'), ('B', 'abc'), ('A', '456'), ('A', '7'), ('B', 'd')]
Run Code Online (Sandbox Code Playgroud)

现在,您可以迭代返回的元组,并使用适当的标记标记每个元组.

您可以更进一步,使用类来捕获类型和特定于类型的解析后逻辑,然后将类作为表达式的解析操作传递.这将在返回的ParseResults中创建类的实例,然后可以使用某种execdoIt方法直接执行:

class ResultsHandler(object):
    """Define base class to initialize location and tokens.
       Call subclass-specific post_init() if one is defined."""
    def __init__(self, s,locn,tokens):
        self.locn = locn
        self.tokens = tokens
        if hasattr(self, "post_init"):
            self.post_init()

class AHandler(ResultsHandler):
    """Handler for A expressions, which contain a numeric string."""
    def post_init(self):
        self.int_value = int(self.tokens[0])
        self.odd_even = ("EVEN","ODD")[self.int_value % 2]
    def doIt(self):
        print "An A-Type was found at %d with value %d, which is an %s number" % (
                self.locn, self.int_value, self.odd_even)

class BHandler(ResultsHandler):
    """Handler for B expressions, which contain an alphabetic string."""
    def post_init(self):
        self.string = self.tokens[0]
        self.vowels_count = sum(self.string.lower().count(c) for c in "aeiou")
    def doIt(self):
        print "A B-Type was found at %d with value %s, and contains %d vowels" % (
                self.locn, self.string, self.vowels_count)


# pass expression-specific handler classes as parse actions
A = Word(nums).setParseAction(AHandler)
B = Word(alphas).setParseAction(BHandler)
expr = OneOrMore(A | B)

# parse string and run handlers
result = expr.parseString("123 abc 456 7 d")
for handler in result:
    handler.doIt()
Run Code Online (Sandbox Code Playgroud)

打印:

An A-Type was found at 0 with value 123, which is an ODD number
A B-Type was found at 4 with value abc, and contains 1 vowels
An A-Type was found at 8 with value 456, which is an EVEN number
An A-Type was found at 12 with value 7, which is an ODD number
A B-Type was found at 14 with value d, and contains 0 vowels
Run Code Online (Sandbox Code Playgroud)