re.sub 替换为匹配的内容
为了掌握 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
是执行此操作的正确函数吗?
Trying to get to grips with regular expressions in Python, I'm trying to output some HTML highlighted in part of a URL. My input is
images/:id/size
my output should be
images/<span>:id</span>/size
If I do this in Javascript
method = 'images/:id/size';
method = method.replace(/\:([a-z]+)/, '<span>$1</span>')
alert(method)
I get the desired result, but if I do this in Python
>>> method = 'images/:id/huge'
>>> re.sub('\:([a-z]+)', '<span>$1</span>', method)
'images/<span>$1</span>/huge'
I don't, how do I get Python to return the correct result rather than $1
? Is re.sub
even the right function to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需使用
\1
而不是$1
:另请注意 原始字符串 (
r'...'
) 用于正则表达式。它不是强制性的,但消除了转义反斜杠的需要,可以说使代码更具可读性。Simply use
\1
instead of$1
:Also note the use of raw strings (
r'...'
) for regular expressions. It is not mandatory but removes the need to escape backslashes, arguably making the code slightly more readable.整个匹配值的反向引用是
\g<0>
,请参阅re.sub
文档:请参阅 Python 演示:
如果您需要执行不区分大小写的搜索,请添加
flag=re.I< /代码>:
A backreference to the whole match value is
\g<0>
, seere.sub
documentation:See the Python demo:
If you need to perform a case insensitive search, add
flag=re.I
:使用
\1
而不是$1
。http://docs.python.org/library/re.html#regular -表达式语法
Use
\1
instead of$1
.http://docs.python.org/library/re.html#regular-expression-syntax
对于替换部分,Python 使用 sed 和 vi 的方式使用
\1
,而不是像 Perl、Java 和 Javascript(以及其他)那样使用$1
) 做。此外,由于\1
在常规字符串中插入字符 U+0001,因此您需要使用原始字符串或 \escape 它。For the replacement portion, Python uses
\1
the way sed and vi do, not$1
the way Perl, Java, and Javascript (amongst others) do. Furthermore, because\1
interpolates in regular strings as the character U+0001, you need to use a raw string or \escape it.