Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 匹配字符串和提取部分_Python_Regex - Fatal编程技术网

Python 匹配字符串和提取部分

Python 匹配字符串和提取部分,python,regex,Python,Regex,我这里有个问题。我有一个字符串,我想用正则表达式提取它的一部分。这是绳子 {{name}我爱我的一些作品{{{hero} 我想提取 [{{name},{{{hero}}] 同样在字符串作为 {{name}我爱我一些作品{{{英雄,进来{这里,这是对的} 我还是想去 [{{name},{{name,{here,right}] 我希望这是有意义的。我正在使用Python。您尝试过以下方法吗 import re s = '{{name} I love me some work {{hero, come

我这里有个问题。我有一个字符串,我想用正则表达式提取它的一部分。这是绳子

{{name}我爱我的一些作品{{{hero}

我想提取

[{{name},{{{hero}}]

同样在字符串作为

{{name}我爱我一些作品{{{英雄,进来{这里,这是对的}

我还是想去

[{{name},{{name,{here,right}]


我希望这是有意义的。我正在使用Python。

您尝试过以下方法吗

import re
s = '{{name} I love me some work {{hero, come in {here, this is right}'
print re.findall(r'\{.*?\}', s)

如果你想要
[{{name}}',{{{hero}}',{{{hero,进来{这里,这是正确的}]
使用@Avinash的正则表达式


如果您想要
[{{name}}'、{{{hero}}'、{{{hero',{here',right}']
请使用以下命令:

re.findall(r'{+\w+}*|{*\w+}+', s)

第二个字符串有不平衡的括号。你确定吗?嘿@vks,是的,我确定。我的想法是也提取它。我想对结果进行验证,以确保没有人发送带有不平衡括号的字符串。请定义你想如何从{{{name}获取我爱我一些作品{{{hero,进来{这里,这是正确的}到
[{{{name},{{{name,{here,right}]
嗨@pacholik,有人已经帮我解决了。非常感谢你。嗨@siwel,谢谢你的回答,我试过了,但没有抓住{{name}我爱我的一些作品{{{hero,进来{这里,这是正确的},我们去跳舞怎么样}嗯。它遗漏了一些部分。谢谢@karthik,你的解决方案确实解决了我的问题。再次感谢你。