【发布时间】:2011-11-03 16:59:34
【问题描述】:
为了掌握 Python 中的正则表达式,我试图输出一些在 URL 的一部分中突出显示的 HTML。我的意见是
images/:id/size
我的输出应该是
images/<span>:id</span>/size
如果我在 Javascript 中这样做
method = 'images/:id/size';
method = method.replace(/\:([a-z]+)/, '<span>$1</span>')
alert(method)
我得到了想要的结果,但是如果我在 Python 中这样做
>>> method = 'images/:id/huge'
>>> re.sub('\:([a-z]+)', '<span>$1</span>', method)
'images/<span>$1</span>/huge'
我不知道,如何让 Python 返回正确的结果而不是 $1? re.sub 甚至是执行此操作的正确函数吗?
【问题讨论】:
标签: python regex re python-regex