如何用字符串仅替换字符串中的整数
我的脚本:
x="the number 1234, today the 909090 of the value of the 90.94"
# by the way the has to be proposed to get the123abd 123the" by a string "ItWasanINT""
re.sub(r'\b\d+(?!\.\d+)\b','ItWasanINT',x)
...不工作
如何用字符串仅替换字符串中的整数
我的脚本:
x="the number 1234, today the 909090 of the value of the 90.94"
# by the way the has to be proposed to get the123abd 123the" by a string "ItWasanINT""
re.sub(r'\b\d+(?!\.\d+)\b','ItWasanINT',x)
...不工作
您可能希望排除前面都没有“数字”的数字。组合并且后面没有“.number”组合:
re.sub(r'\b(?<!\d\.)\d+(?!.\d)\b','ItWasanINT', x)
结果:
>>> import re
>>> x="the number 1234, today the 909090 of the value of the 90.94"
>>> re.sub(r'\b(?<!\d\.)\d+(?!\.\d)\b','ItWasanINT',x)
'the number ItWasanINT, today the ItWasanINT of the value of the 90.94'