python - 在 Python 中使用正则表达式作为模板

标签 python regex templates

我有使用正则表达式模式作为模板的想法,想知道在 Python(3 或更新版本)中是否有一种方便的方法来这样做。

import re

pattern = re.compile("/something/(?P<id>.*)")
pattern.populate(id=1) # that is what I'm looking for

应该导致

/something/1

最佳答案

这不是正则表达式的用途,您可以使用普通的字符串格式。

>>> '/something/{id}'.format(id=1)
'/something/1'

关于python - 在 Python 中使用正则表达式作为模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3672244/

相关文章:

python - 确定目录中是否添加、删除或修改了任何文件

javascript - 在JavaScript中,将csv转换为json时如何忽略空行

C++ 委托(delegate)练习

html - gmail html 模板的当前 CSS 状态是什么?

python - Pyephem Alt。和 Az 是完全错误的

python - 子进程在终止前获取结果

python - py2exe:生成的 EXE 未加载

java - 使用 Regex/Split() 有效分割 <endpoint1>,<endpoint2> (?)

python - 如何从 python 命令行接收正则表达式

javascript - 如何在不单击的情况下激活此 JQuery fancybox?