我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用pyparsing.CharsNotIn()。
def parse_format(format): definition = [] # define pattern grammar variable_ptn = pp.QuotedString("{", endQuoteChar="}")("variable") escape_open_ptn = pp.Literal("{{")("escape_open") escape_close_ptn = pp.Literal("}}")("escape_close") escape_ptn = escape_open_ptn | escape_close_ptn literal_ptn = pp.CharsNotIn("{}")("literal") element_ptn = escape_ptn | variable_ptn | literal_ptn for toks, start, end in element_ptn.leaveWhitespace().scanString(format): try: definition.append({ "literal": lambda: Literal(toks[0]), "variable": lambda: Variable.create(toks[0]), "escape_open": lambda: OpenBrace(), "escape_close": lambda: CloseBrace(), }[toks.items()[0][0]]()) except KeyError: raise FormatStringError return definition
def __init__(self, rule): self.rule = rule self.opener = self.rule['opener'] self.closer = self.rule['closer'] self.columns = self.rule.get('columns', -1) nested = nestedExpr(opener=self.opener, closer=self.closer, content=CharsNotIn(self.opener + self.closer)) if self.columns < 0: self.nested = OneOrMore(nested) else: self.nested = nested * self.columns + Or([CharsNotIn('\n'), Empty()])