stringa = \”苹果 32 公斤\”
TextDict[\’apple\’] = re.sub(r\”\\d+公斤\”,r\”[\\d+]公斤\”,stringa)
理论上 TextDict[\’apple\’]应该是\”苹果[32]公斤\”
发现结果不是,是 sub 的语句错了么?
Click to rate this post!
[Total: 0 Average: 0]
stringa = \”苹果 32 公斤\”
TextDict[\’apple\’] = re.sub(r\”\\d+公斤\”,r\”[\\d+]公斤\”,stringa)
理论上 TextDict[\’apple\’]应该是\”苹果[32]公斤\”
发现结果不是,是 sub 的语句错了么?
re.sub(r”(\d+)公斤”,r”[$1]公斤”,stringa)
“`
re.sub(r”(\d+)公斤”, r”[\1]公斤”, stringa)
“`
建议重读文档: https://docs.python.org/3/library/re.html#re.sub
nnd,老是忘了 py 用 \,不用$
#2 正确,#1 写错了
是想给数字加括号?
中括号要转义