[英]Python regex - match everything not in mustache brackets
I'm trying to create a regex in Python to match everything not in mustache brackets. 我正在尝试在Python中创建一个正则表达式,以匹配胡子括号中的所有内容。
For example, this string: 例如,这个字符串:
This is an {{example}} for {{testing}}.
should produce this list of strings: 应该产生这个字符串列表:
["This is an ", " for ", "."]
when used with re.findall
. 与
re.findall
使用时。
For my mustache-matching regex, I am using this: {{(.*?)}}
. 对于我的胡子匹配正则表达式,我使用这个:
{{(.*?)}}
。
It seems like it should just be a simple matter of negating the above pattern, but I can't get it to work properly. 看起来它应该只是一个简单的否定上述模式的问题,但我无法让它正常工作。 I'm testing using: http://pythex.org
我正在测试使用: http : //pythex.org
Thanks. 谢谢。
你需要拆分
re.split('{{.*?}}', s)
@Andrey solution is the most simple and clearly the way to go. @Andrey解决方案是最简单明了的方法。 You have another possible way with
findall
because when the pattern contains a capture group, it returns only the capture group content: findall
还有另一种可能的方法,因为当模式包含捕获组时,它只返回捕获组内容:
re.findall(r'(?:{{.*?}})*([^{]+)', s)
So, if you start the pattern with an optional non-capturing group for curly brackets parts followed by a capture group for other content, findall
returns only the capture group content and all curly brackets parts are consumed. 因此,如果您使用可选的非捕获组启动模式,对于大括号部分,后跟其他内容的捕获组,则
findall
仅返回捕获组内容,并且消耗所有大括号部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.