【发布时间】:2017-02-28 08:18:05
【问题描述】:
以下表达式可以很好地提取data 字符串中以单词Block 开头、后跟左括号{ 并以右括号'}' 结尾的部分:
data ="""
Somewhere over the rainbow
Way up high
Block {
line 1
line 2
line 3
}
And the dreams that you dreamed of
Once in a lullaby
"""
regex = re.compile("""(Block\ {\n\ [^\{\}]*\n}\n)""", re.MULTILINE)
result = regex.findall(data)
print result
返回:
['Block {\n line 1\n line 2\n line 3\n}\n']
但如果字符串的 Block 部分内有另一个大括号,则表达式会中断,返回一个空列表:
data ="""
Somewhere over the rainbow
Way up high
Block {
line 1
line 2
{{}
line 3
}
And the dreams that you dreamed of
Once in a lullaby
Block {
line 4
line 5
{{
}
line 6
}
Somewhere over the rainbow
Blue birds fly
And the dreams that you dreamed of
Dreams really do come true ooh oh
"""
如何修改此正则表达式以使其忽略 Blocks 内的括号,但每个块都作为 result 列表中的单独实体返回(因此可以单独访问每个 Block)?
【问题讨论】:
-
实际上
[^{}]*阻止匹配任何左大括号。请注意,MULTILINE 标志不是您想的那样。 (它不是为了匹配分布在多行的字符串,它只会改变锚点^和$的含义。为了让点匹配换行符,标志是DOTALL) -
既然你编辑了你的问题:这个问题不能用 re 模块解决,你需要处理递归的 regex 模块。请注意,您需要为未确定的情况选择默认行为。