简体   繁体   English

re.sub 替换为匹配的内容

[英]re.sub replace with matched content

Trying to get to grips with regular expressions in Python, I'm trying to output some HTML highlighted in part of a URL.试图掌握 Python 中的正则表达式,我试图输出一些在 URL 的一部分中突出显示的 HTML。 My input is我的输入是

images/:id/size

my output should be我的输出应该是

images/<span>:id</span>/size

If I do this in Javascript如果我在 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我得到了想要的结果,但是如果我在 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 ?我不知道,如何让 Python 返回正确的结果而不是$1 Is re.sub even the right function to do this? re.sub甚至是执行此操作的正确功能吗?

Simply use \\1 instead of $1 :只需使用\\1而不是$1

In [1]: import re

In [2]: method = 'images/:id/huge'

In [3]: re.sub(r'(:[a-z]+)', r'<span>\1</span>', method)
Out[3]: 'images/<span>:id</span>/huge'

Also note the use of raw strings ( r'...' ) for regular expressions.另请注意将原始字符串( r'...' ) 用于正则表达式。 It is not mandatory but removes the need to escape backslashes, arguably making the code slightly more readable.它不是强制性的,但不需要转义反斜杠,可以说使代码更具可读性。

Use \\1 instead of $1 .使用\\1而不是$1

\\number Matches the contents of the group of the same number. \\number 匹配同号组的内容。

http://docs.python.org/library/re.html#regular-expression-syntax http://docs.python.org/library/re.html#regular-expression-syntax

A backreference to the whole match value is \\g<0> , see re.sub documentation :对整个匹配值的反向引用是\\g<0> ,请参阅re.sub文档

The backreference \\g<0> substitutes in the entire substring matched by the RE.反向引用\\g<0>替换 RE 匹配的整个子字符串。

See the Python demo :请参阅Python 演示

import re
method = 'images/:id/huge'
print(re.sub(r':[a-z]+', r'<span>\g<0></span>', method))
# => images/<span>:id</span>/huge

For the replacement portion, Python uses \\1 the way sed and vi do, not $1 the way Perl, Java, and Javascript (amongst others) do.对于替换部分,Python 像 sed 和 vi 那样使用\\1而不是像 Perl、Java 和 Javascript(以及其他)那样使用$1 Furthermore, because \\1 interpolates in regular strings as the character U+0001, you need to use a raw string or \\escape it.此外,因为\\1在常规字符串中插入字符 U+0001,所以您需要使用原始字符串或 \\escape 它。

Python 3.2 (r32:88445, Jul 27 2011, 13:41:33) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> method = 'images/:id/huge'
>>> import re
>>> re.sub(':([a-z]+)', r'<span>\1</span>', method)
'images/<span>id</span>/huge'
>>> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM