从括号中提取文本

问题描述 投票:-2回答:2

如何从以下字符串中提取括号内的文本:

string =  '{a=[], b=[abc, def], c=[ghi], d=[], e=[jkl], f=[mno, pqr, stu, vwx]}'

预期产出是:

['abc','def','ghi','jkl','mno','pqr','stu','vwx']
python regex
2个回答
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
投票

使用较新的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

a demo on regex101.com


In 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']
© www.soinside.com 2019 - 2024. All rights reserved.