【发布时间】:2019-10-24 04:43:09
【问题描述】:
我的输入格式如下(txt1):
txt1 = "[('1','Hello is 1)people 2)animals'), ('People are 1) hello 2) animals'), ('a')]"
我想把它提取成以下格式-
[['1','Hello is 1)people 2)animals'],['People are 1) hello 2) animals'],['a']]
所以,基本上,我想要括号内的信息。但我无法做到这一点。此外,我使用了 Lookahead 和 Lookbehind 来避免被数字拆分 - '1)' 或 '2)' 之前我在re.split('[\(\)\[\]] 的简单语句中发生了这种情况
我一直在尝试使用 findall 功能来检查我得到了什么。
r = re.findall(r'\((?=\').*(?<=\')\)(?=\,)', txt1)
我得到了-
["('1','Hello is 1)people 2)animals'), ('People are 1) hello 2) animals')"]
似乎忽略了中间括号。我该怎么做才能得到我需要的结果?
谢谢。
注意:
对于我打算用来获得所需输出的拆分功能,我得到了这个-
r = re.split(r'\((?=\').*(?<=\')\)(?=\,)', txt1)
['[', ", ('a')]"]
【问题讨论】:
标签: python arrays regex python-3.x regex-lookarounds