提取括号内的文本并存储在字典中

Extract text inside brackets and store in dictionary

我试图将方括号内的所有函数分开,并将它们存储在字典中。但是,输出会从除最后一个输出之外的所有输出中去除右括号。

import re
line="[f(x,y),g(y,z),f1(x1,y1)]"
matches = re.match(r"(.*)(\[)(.*)(\])(.*)", line)
if matches:
    all_action_labels = matches.group(3)
    sep_action_labels = re.split(r'\),',all_action_labels)
    j=0
    for x in sep_action_labels:
        print(f'Function #{j+1} : {x}')

如您所见,所有输出都缺少右括号')',除了最后一个:

Function #1 : f(x,y
Function #1 : g(y,z
Function #1 : f1(x1,y1)

我应该使用什么正则表达式?

此外,如何将这些输出存储在字典中?

我提取数据的一般规则是使用相当简单的正则表达式调用 re.findall()

也许这符合您的需求:

import re
line="[f(x,y),g(y,z),f1(x1,y1)]"
all_action_labels = re.findall(r"\[(.*?)]", line)
for all_action_label in all_action_labels:
    sep_action_labels = re.findall(r"[a-z0-9]+\(.*?\)", all_action_label)
    for j, x in enumerate(sep_action_labels, 1):
        print(f'Function #{j} : {x}')

我使用一个简单的正则表达式从 [] 中提取数据,并使用另一个来提取各个函数调用。

如果您不需要使用正则表达式,这样做可能会更容易。这很容易理解,它只是遍历字符串,并将函数字符串放入列表中,并且跟踪括号,因此可以很好地处理具有多个逗号的函数。

def getFuncList(line):
  """
  Assumes comma seperated, and opends and closes with square brackets
  """
  line = line[1:-1] # strip square brackets
  funcs = []

  current = ""
  brack_stack = 0 # we don't want to follow comma's if they are in a function
  for char in line:
    if char == "(":
      brack_stack += 1 
    elif char == ")":
      brack_stack -= 1 

    if char == "," and brack_stack == 0:
      # new function, clear current and append to list
      funcs.append(current)
      current = ""
    else:
      current += char
  funcs.append(current)
  return funcs


line="[f(x,y),g(y,z),f1(x1,y1)]"
func_list = (getFuncList(line))
print({"Function "+str(x+1): func_list[x] for x in range(len(func_list))}) # make and print the dictionary
# {'Function 1': 'f(x,y)', 'Function 2': 'g(y,z)', 'Function 3': 'f1(x1,y1)'}