假设我们有这样的情况:

>>> a = "test string with %(experiment1)s and %(experiment2)s"


有没有办法提取这样的列表?

['experiment1', 'experiment2']


谢谢!

最佳答案

使用regex

>>> import re
>>> a = "test string with %(experiment1)s and %(experiment2)s"
>>> re.findall(r'%\((.*?)\)', a)
['experiment1', 'experiment2']

09-27 22:35