python - 您如何替换后跟数字字符的 Python 捕获?

标签 python regex string

使用 re.sub 时,您如何处理需要捕获后跟替换字符串中的数字的情况?例如,您不能将“\10”用于后跟“0”字符的捕获 1,因为它将被解释为捕获 10。

最佳答案

\g<1>0

http://docs.python.org/library/re.html#re.sub

\g<number> uses the corresponding group number; \g<2> is therefore equivalent to \2, but isn’t ambiguous in a replacement such as \g<2>0. \20 would be interpreted as a reference to group 20, not a reference to group 2 followed by the literal character '0'.

关于python - 您如何替换后跟数字字符的 Python 捕获?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/650926/

相关文章:

python - Django:管理界面中多对多项目的名称

python - 从python列表中删除特定元素

python - 使用 PickleProperty 在 GAE 中存储字典

python - argparse 选择允许值的结构

python - 我可以使用 python 're' 来解析复杂的人名吗?

javascript - 用于将数字与两个和三个编号模式一起匹配的正则表达式

javascript - 用于验证十进制数的 JavaScript 中的正则表达式

c++ - 反转字符串 C++

c - 如何在c中剪切字符串的一部分?

java - 关于排除注释行