gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:54:17 24 4
gpt4 key购买 nike

使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com