我想为下面的例子写一个Python2正则表达式我还没有做可行性分析:
the quick [brown_1=fox] jumps over the [lazy=dog]
["[brown_1=fox]", "[lazy=dog]"]
基本上,我希望匹配所有[\w+=.*]
子字符串,并将它们输出到一个列表中。看起来我想在这里使用的API是re.findall
,但是在Python2中什么是合适的正则表达式呢
在这种特定情况下,子字符串[brown_1=fox]
和[lazy=dog]
分别与regex:[\w+=.*]
匹配
注意:
允许以下输入字符串:the quick [brown=[fox[a=b][c=d]]] jumps over
。i、 e.在匹配[\w+=.*]
的单个元素中,[
、]
和=
字符可能再次出现。但可以保证,在第一个“等于”字符之前,只有字母数字字符和下划线
我想你需要一个简单的状态机
您可以安装替代的^{} module ,它支持递归模式^{} ,这样您就可以像这样使用
findall()
方法:这将输出:
相关问题 更多 >
编程相关推荐