Python 3.x 获取python中的所有%(名称)占位符
我有一个包含Python 3.x 获取python中的所有%(名称)占位符,python-3.x,Python 3.x,我有一个包含%(name)s占位符的字符串,我想获取所有的名称,例如:这是一个%(name)s示例字符串%(foo)s我想%(bar)s从%(place)s中提取所有占位符 我想获得一个包含名称,foo,条,位置,使用正则表达式的列表 Ex: import re s = "This is a %(name)s example string %(foo)s I would like %(bar)s to extract all the placeholders from %(place)s" p
%(name)s
占位符的字符串,我想获取所有的名称,例如:这是一个%(name)s示例字符串%(foo)s我想%(bar)s从%(place)s中提取所有占位符
我想获得一个包含名称
,foo
,条
,位置
,使用正则表达式的列表
Ex:
import re
s = "This is a %(name)s example string %(foo)s I would like %(bar)s to extract all the placeholders from %(place)s"
print(re.findall(r"%\((.*?)\)", s))
# --> ['name', 'foo', 'bar', 'place']
似乎是一个很好的使用
%\((.+?)\)
表示从%(
)开始,以下一个)
结束,捕获中间的所有内容
>>> import re
>>> string = "This is a %(name)s example string %(foo)s I would like %(bar)s to extract all the placeholders from %(place)s"
>>> re.findall(r'%\((.+?)\)', string)
['name', 'foo', 'bar', 'place']