Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中解析嵌套表达式以检索列表中的每个函数_Python_Parsing - Fatal编程技术网

在python中解析嵌套表达式以检索列表中的每个函数

在python中解析嵌套表达式以检索列表中的每个函数,python,parsing,Python,Parsing,假设我有一个如下所示的表达式,我想解析它并在列表中按升序提取内部表达式 expression = DATENAME('weekday',DATEADD('month',3,[OrderDate])) 我希望输出为 ["DATEADD('month',3,[OrderDate])","DATENAME('weekday',DATEADD('month',3,[OrderDate]))"] ["'weekday',DATEADD('month',

假设我有一个如下所示的表达式,我想解析它并在列表中按升序提取内部表达式

expression = DATENAME('weekday',DATEADD('month',3,[OrderDate]))
我希望输出为

["DATEADD('month',3,[OrderDate])","DATENAME('weekday',DATEADD('month',3,[OrderDate]))"]
["'weekday',DATEADD('month',3,[OrderDate])", "DATENAME('weekday',DATEADD('month',3,[OrderDate]))"]
但我得到的输出是

["DATEADD('month',3,[OrderDate])","DATENAME('weekday',DATEADD('month',3,[OrderDate]))"]
["'weekday',DATEADD('month',3,[OrderDate])", "DATENAME('weekday',DATEADD('month',3,[OrderDate]))"]
这就是我尝试过的

result = []
expression = "DATENAME('weekday',DATEADD('month',3,[OrderDate]))"
for i in range(len(expression)):
    for j in range(len(expression)):
        if expression[i:j+1].count('(') == expression[i:j+1].count(')') != 0:
            if (expression[i-1] == '(' or i == 0) and expression[j] == ')':
                result.append(expression[i:j+1])
result.reverse()
printg(result)
对于下面的表达式,我的代码返回正确的结果

CONTAINS((Replace(Lower(UPPER([ProductName]+[ProductName])),'chaichai','chai')),'chai')
['UPPER([ProductName]+[ProductName])', 'Lower(UPPER([ProductName]+[ProductName]))', "Replace(Lower(UPPER([ProductName]+[ProductName])),'chaichai','chai')", "(Replace(Lower(UPPER([ProductName]+[ProductName])),'chaichai','chai'))", "CONTAINS((Replace(Lower(UPPER([ProductName]+[ProductName])),'chaichai','chai')),'chai')"]

请帮助

遵循代码可能会这样做


s="CONTAINS([Quantity (Products)],'10')"

fangKuoHaoPairsIndex=[]
fangKuoHaoPairsFromIndex=[]
for i in range(len(s)):
    if s[i] == "[":
        fangKuoHaoPairsFromIndex.append(i)
        pass
    elif s[i] == "]":
        fromIdx=fangKuoHaoPairsFromIndex.pop()
        fangKuoHaoPairsIndex.append((fromIdx,i))

for i in range(len(s)):
    if s[i]=="(":
        a.append(i)

    elif s[i]==")":
        fromIdx=a.pop()

        fromIdx2=max(a[-1],s.rfind(",", 0, fromIdx))

        flag=False
        for (fromIndex, toIndex) in fangKuoHaoPairsIndex:
            if fromIdx2 + 1 >= fromIndex and i <= toIndex:
                flag=True
                break
        if flag==False:
            print(s[fromIdx2+1:i+1])

s=“包含([数量(产品)],'10')”
方国浩PairIndex=[]
方国浩PairsFromIndex=[]
对于范围内的i(len(s)):
如果s[i]==“[”:
方国浩PairsFromIndex.append(一)
通过
elif s[i]==“]”:
fromIdx=FangkuaoPairsFromIndex.pop()
方国浩PairIndex.append((fromIdx,i))
对于范围内的i(len(s)):
如果s[i]==“(”:
a、 附加(i)
elif s[i]==”):
fromIdx=a.pop()
fromIdx2=max(a[-1],s.rfind(“,”,0,fromIdx))
flag=False
对于方国浩派索引中的(fromIndex,toIndex):

如果fromIdx2+1>=fromIndex和我有任何帮助,请接受,但是使用这个表达式
包含([Quantity(Products)],'10')
它将输出返回为
[Quantity(Products)
包含([Quantity(Products)],'10')
。输出应该是
包含([Quantity(Products)],'10')
为什么不想要输出“Quantity(Products)”?现在我很困惑Quantity(Products)不是一个函数。它是函数中的一个参数表达式=DATENAME('weekday',DATEADD('month',3,[OrderDate]))DATEADD也是DATENAME函数的一个参数,但如果表达式包含(Quantity(Products),'10',为什么要输出“DATEADD('month',3,[OrderDate])”,是否应输出“数量(产品)”?日期添加其参数和函数名。。而“数量(产品)”仅是包含在[]中的参数(表名)