我有一个这样的段落:

paragraph = "Dear {{userName}},

You have been registered successfully. Our Manager {{managerName}} will contact soon.

Thanks"


我需要解析{{}}中的所有字符串。本段中可能还有更多类似内容。

我尝试了以下解决方案:

result = re.search('{{(.*)}}',paragraph)
print(result.group(1))
# output is 'userName}} {{ManagerName'


我想要的输出是:

["userName","managerName",....]


请帮忙。

谢谢

最佳答案

使用re.findall

例如:

import re

paragraph = """Dear {{userName}},
You have been registered successfully. Our Manager {{managerName}} will contact soon.
Thanks"""

print( re.findall(r"\{\{(.*?)\}\}", paragraph) )


输出:

['userName', 'managerName']

07-24 09:17