假设我有一个像str = "[Hi all], [this is] [an example] "
这样的字符串。我想将它分成几个部分,每个部分包含一对括号内的内容。换句话说,我想抓住每对支架内的短语。结果应该是:
['Hi all', 'this is', 'an example']
如何使用Python中的正则表达式实现此目标?
答案 0 :(得分:11)
data = "[Hi all], [this is] [an example] "
import re
print re.findall("\[(.*?)\]", data) # ['Hi all', 'this is', 'an example']
答案 1 :(得分:2)
试试这个:
import re
str = "[Hi all], [this is] [an example] "
contents = re.findall('\[(.*?)\]', str)
答案 2 :(得分:1)
我已经遇到过几次这个问题-除非您有方括号,否则正则表达式将起作用。在您可能有嵌套方括号的更一般的情况下,以下方法将起作用:
def bracketed_split(string, delimiter, strip_brackets=False):
""" Split a string by the delimiter unless it is inside brackets.
e.g.
list(bracketed_split('abc,(def,ghi),jkl', delimiter=',')) == ['abc', '(def,ghi)', 'jkl']
"""
openers = '[{(<'
closers = ']})>'
opener_to_closer = dict(zip(openers, closers))
opening_bracket = dict()
current_string = ''
depth = 0
for c in string:
if c in openers:
depth += 1
opening_bracket[depth] = c
if strip_brackets and depth == 1:
continue
elif c in closers:
assert depth > 0, f"You exited more brackets that we have entered in string {string}"
assert c == opener_to_closer[opening_bracket[depth]], f"Closing bracket {c} did not match opening bracket {opening_bracket[depth]} in string {string}"
depth -= 1
if strip_brackets and depth == 0:
continue
if depth == 0 and c == delimiter:
yield current_string
current_string = ''
else:
current_string += c
assert depth == 0, f'You did not close all brackets in string {string}'
yield current_string
>>> list(bracketed_split("[Hi all], [this is] [an example]", delimiter=' '))
['[Hi all],', '[this is]', '[an example]']
>>> list(bracketed_split("[Hi all], [this is] [a [nested] example]", delimiter=' '))
['[Hi all],', '[this is]', '[a [nested] example]']