如何在pyparsing中匹配括号/括号 [英] How to match parentheses / brackets in pyparsing

查看:123
本文介绍了如何在pyparsing中匹配括号/括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个语法标记,指定为:

I have a grammar token specified as:

list_value = Suppress(oneOf("[ (")) + Group(
    delimitedList(string_value | int_value))("list") + Suppress(oneOf("] )"))

但是,这显然允许(foo, bar]

如何强制列表的开头和结尾字符必须匹配?

How do I enforce that the lists opening and closing characters must match?

推荐答案

您在两个规则之间做出选择:一个用于括号,一个用于方括号.感谢您提出pyparsing.我喜欢.我对您的问题的回答是:

You make a list a choice between two rules: one for parentheses and one for square brackets. Thanks for bringing up pyparsing. I like it. My answer for your question is:

delim_value = Group(delimitedList(string_value | int_value))("list")
list_value = Or( (Suppress("[") + delim_value + Suppress("]"),
                  Suppress("(") + delim_value + Suppress(")")) )

这篇关于如何在pyparsing中匹配括号/括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆