Python Pyparsing:捕获括号内的逗号分隔列表,忽略内部括号

Python Pyparsing: Capture comma-separated list inside parentheses ignoring inner parentheses

我对如何正确解析如下字符串有疑问,

"(test.function, arr(3,12), "combine,into one")"

进入以下列表,

['test.function', 'arr(3,12)', '"combine,into one"']

注意:原始字符串中的'list'项不一定是用逗号和space分隔的,也可以是两个直接用逗号分隔的项,例如test.function,arr(3,12).

基本上,我想:

  1. 解析括号中包含的输入字符串,但不解析内括号。 (因此,nestedExpr() 不能按原样使用)
  2. 里面的项目用逗号分隔,但项目本身可能包含逗号。

另外,我只能用scanString(),不能用parseString()

我在 SO 中进行了一些搜索并找到了 and this,但我无法将它们翻译成适合我的问题。

谢谢!

这应该可以解决您的嵌套和引用问题:

sample = """(test.function, arr(3,12),"combine,into one")"""

from pyparsing import (Suppress, removeQuotes, quotedString, originalTextFor, 
    OneOrMore, Word, printables, nestedExpr, delimitedList)

# punctuation and basic elements
LPAR,RPAR = map(Suppress, "()")
quotedString.addParseAction(removeQuotes)

# what are the possible values inside the ()'s?
# - quoted string - anything is allowed inside quotes, match these first
# - any printable, not containing ',', '(', or ')', with optional nested ()'s
#   (use originalTextFor helper to extract the original text from the input
#   string)
value = (quotedString 
         | originalTextFor(OneOrMore(Word(printables, excludeChars="(),") 
                                     | nestedExpr())))

# define an overall expression, with surrounding ()'s
expr = LPAR + delimitedList(value) + RPAR

# test against the sample
print(expr.parseString(sample).asList())

打印:

['test.function', 'arr(3,12)', 'combine,into one']