>>> from re import findall
>>> line = "Hello my name is {{ name }} and I am {{ age }} years old."
>>> findall("{{\s(.*?)\s}}", line)
['name', 'age']
>>>
下面是正则表达式模式的细分:
{{ # {{
\s # A space
(.*?) # A capture group of zero or more characters matched non-greedily
\s # A space
}} # }}
>>> from re import findall
>>> line = "Hello my name is {{name }} and I am {{ age }} years old."
>>> findall("{{\s*(.*?)\s*}}", line)
['name', 'age']
>>>
您可以使用^{} :
下面是正则表达式模式的细分:
如果您想了解更多信息,以上使用的所有Regex语法都将在here中解释。你知道吗
请注意,如果空格的长度可能不同,则可能需要将
\s
替换为\s*
,这将匹配零个或多个空格:相关问题 更多 >
编程相关推荐