假设我得到以下类型的字符串:
"(this is (haha) a string(()and it's sneaky)) ipsom (lorem) bla"
,我想提取包含在最顶层括号中的子字符串。例如,我想获得字符串:"this is (haha) a string(()and it's sneaky)"
和"lorem"
。
是否有一个很好的python方法来做到这一点?正则表达式显然不能完成这项任务,但也许有一种方法可以让xml解析器完成这项工作?对于我的应用程序,我可以假设括号是格式良好的,即不像(()()。
这是堆栈的标准用例:您按字符读取字符串,每当遇到左括号时,将符号推入堆栈;如果遇到右括号,则将该符号从堆栈中弹出。
因为你只有一种类型的圆括号,你实际上不需要堆栈;相反,只要记住有多少个开括号就足够了。
此外,为了提取文本,我们还记得在第一级括号打开时部分开始的位置,并在遇到匹配的右括号时收集结果字符串。
可以像这样:
string = "(this is (haha) a string(()and it's sneaky)) ipsom (lorem) bla"
stack = 0
startIndex = None
results = []
for i, c in enumerate(string):
if c == '(':
if stack == 0:
startIndex = i + 1 # string to extract starts one index later
# push to stack
stack += 1
elif c == ')':
# pop stack
stack -= 1
if stack == 0:
results.append(string[startIndex:i])
print(results)
# ["this is (haha) a string(()and it's sneaky)", 'lorem']
这不是很"python "…但
def find_strings_inside(what_open,what_close,s):
stack = []
msg = []
for c in s:
s1=""
if c == what_open:
stack.append(c)
if len(stack) == 1:
continue
elif c == what_close and stack:
stack.pop()
if not stack:
yield "".join(msg)
msg[:] = []
if stack:
msg.append(c)
x= list(find_strings_inside("(",")","(this is (haha) a string(()and it's sneaky)) ipsom (lorem) bla"))
print x
你确定regex不够好吗?
>>> x=re.compile(r'((?:(?:(.*?))|(?:[^()]*?)))')
>>> x.findall("(this is (haha) a string(()and it's sneaky)) ipsom (lorem) bla")
["(this is (haha) a string(()and it's sneaky)", '(lorem)']
>>> x.findall("((((this is (haha) a string((a(s)d)and ((it's sneaky))))))) ipsom (lorem) bla")
["((((this is (haha) a string((a(s)d)and ((it's sneaky))", '(lorem)']
这或多或少重复了已经说过的内容,但可能更容易阅读:
def extract(string):
flag = 0
result, accum = [], []
for c in string:
if c == ')':
flag -= 1
if flag:
accum.append(c)
if c == '(':
flag += 1
if not flag and accum:
result.append(''.join(accum))
accum = []
return result
>> print extract(test)
["this is (haha) a string(()and it's sneaky)", 'lorem']