我已经编写了以下代码,并且运行良好。但是其中一种情况失败了。我尝试过但无法解决此问题。
#!/usr/bin/env py
import itertools
import sys
import sympy
import re
import pprint
def extract_next_state(s):
p = re.compile('(\()|(\))')
depth = 0
startindex = None
start_point = False
for m in p.finditer(s):
if m.group(1): # (
depth += 1
print "depth (", depth
if not start_point:
startindex = m.start()
start_point = True
elif m.group(2): # )
depth -= 1
print "depth )", depth
if depth == 0:
return s[startindex:m.end()]
return s[startindex:] + ')' * depth
if __name__ == "__main__":
#data = ['next_state=(~SE&((~B2&D)|(B2&lq))|(SE&SI))']
#data = ['next_state=((~SE&((A1&A2)|(B1&B2)))|(SE&SI)))']
#data = ['next_state=((((~SE&((A1&A2)|(B1&B2)))|(SE&SI)))']
data = ['next_state=(D1&S&!SE)|(!S&(!SE&D0))|(SE&SI))']
data_1 = data[0].split(',')
com = None
for item in data_1:
if item.find('next_state=')!= -1:
item_list = item.split('=')
item_op = extract_next_state(item_list[1])
print item_op
输出:
(D1&S&!SE)
预期的 :
(D1&S&!SE)|(!S&(!SE&D0))|(SE&SI)