python - Python:重复查找两个特殊字符之间的字符串
问题描述
我有这样一段:
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']
推荐阅读
- google-sheets - 使用两列中的值返回新列中的特定值
- scheme - 尝试用新元素替换列表中元素的所有实例 [Racket]
- python - 如何在类的方法中正确运行方法(python)
- jasmine - 量角器 - 无法使用 ElemArrayFinder 循环每个项目
- java - 如何使用 --add-opens 参数来摆脱非法反射访问警告?
- python - 从作业处理器启动线程时,如何跟踪作业工作线程的进度?
- r - Loop character values in ggtitle
- javascript - 如何将身份验证标头传递给其他组件
- android - 错误:程序类型已存在:com.google.android.gms.common.internal.zzb
- docker - 具有相同容器端口的多个 docker 容器连接到同一网络