我有一个很长的字符串,其中有嵌套循环。我想从中提取一个模式。
String_Text:
some random texts......
........................
........................
{{info .................
.....texts..............
...{{ some text }}...... // nested parenthesis 1
........................
...{{ some text }}...... // nested parenthesis 2
........................
}} // End of topmost parenthesis
........................
..again some random text
........................
........................ // can also contain {{ }}
......End of string.
我想提取最上面括号之间的所有文本,即
提取的字符串:
info .................
.....texts..............
...{{ some text }}...... // nested parenthesis 1
........................
...{{ some text }}...... // nested parenthesis 2
........................
图案:
1.) starts with { and can be followed by any number of {.
2.) After that there can be any number of white space.
3.) The first word after that is surely info.
4.) Extract till this bracket is not closed.
到目前为止尝试了什么:
re.findall(r'\{+[^\S\r\n]*info\s*(.*(?:\r?\n.*)*)\}+')
我知道这是错误的,因为这是在字符串中找到 的最后一个实例。有人可以帮我提取这些括号之间的文字吗?时间差
最佳答案
关于python - 正则表达式查找嵌套括号之间的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51965650/