如何从以下字符串中提取括号内的文本:
string = '{a=[], b=[abc, def], c=[ghi], d=[], e=[jkl], f=[mno, pqr, stu, vwx]}'
预期输出为:
['abc','def','ghi','jkl','mno','pqr','stu','vwx']
答案 0 :(得分:1)
正则表达式应该有所帮助。
import re
string = '{a=[], b=[abc, def], c=[ghi], d=[], e=[jkl], f=[mno, pqr, stu, vwx]}'
res = []
for i in re.findall("\[(.*?)\]", string):
res.extend(i.replace(",", "").split())
print res
<强>输出:强>
['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx']
答案 1 :(得分:1)
使用较新的regex
模块的替代方案可能是:
(?:\G(?!\A)|\[)([^][,]+)(?:,\s*)?
分解,这说:
(?:\G(?!\A)|\[) # match either [ or at the end of the last match
([^][,]+) # capture anything not [ or ] or ,
(?:,\s*)? # followed by , and whitespaces, eventually
<小时/>
在Python
:
import regex as re
string = '{a=[], b=[abc, def], c=[ghi], d=[], e=[jkl], f=[mno, pqr, stu, vwx]}'
rx = re.compile(r'(?:\G(?!\A)|\[)([^][,]+)(?:,\s*)?')
output = rx.findall(string)
print(output)
# ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx']