在 re.sub 替换模式中处理对捕获组的反向引用 [英] Handling backreferences to capturing groups in re.sub replacement pattern

查看:66
本文介绍了在 re.sub 替换模式中处理对捕获组的反向引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想取字符串 0.71331, 52.25378 并返回 0.71331,52.25378 - 即只查找一个数字、一个逗号、一个空格和一个数字,然后删除出空间.

这是我当前的代码:

coords = '0.71331, 52.25378'coord_re = re.sub("(\d), (\d)", "\1,\2", coords)打印 coord_re

但这给了我0.7133,2.25378.我做错了什么?

解决方案

您应该将原始字符串用于正则表达式,请尝试以下操作:

coord_re = re.sub(r"(\d), (\d)", r"\1,\2", coords)

使用您当前的代码,替换字符串中的反斜杠正在对数字进行转义,因此您要替换与 chr(1) + "," + chr(2) 等效的所有匹配项:

<预><代码>>>>'\1,\2''\x01,\x02'>>>打印 '\1,\2',>>>print r'\1,\2' # 这就是你真正想要的\1,\2

任何时候您想在字符串中保留反斜杠,请使用 r 前缀,或转义每个反斜杠 (\\1,\\2).

I want to take the string 0.71331, 52.25378 and return 0.71331,52.25378 - i.e. just look for a digit, a comma, a space and a digit, and strip out the space.

This is my current code:

coords = '0.71331, 52.25378'
coord_re = re.sub("(\d), (\d)", "\1,\2", coords)
print coord_re

But this gives me 0.7133,2.25378. What am I doing wrong?

解决方案

You should be using raw strings for regex, try the following:

coord_re = re.sub(r"(\d), (\d)", r"\1,\2", coords)

With your current code, the backslashes in your replacement string are escaping the digits, so you are replacing all matches the equivalent of chr(1) + "," + chr(2):

>>> '\1,\2'
'\x01,\x02'
>>> print '\1,\2'
,
>>> print r'\1,\2'   # this is what you actually want
\1,\2

Any time you want to leave the backslash in the string, use the r prefix, or escape each backslash (\\1,\\2).

这篇关于在 re.sub 替换模式中处理对捕获组的反向引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆