用单词替换各种格式的数字

时间:2018-06-06 11:56:40

标签: python

我有一个长句,我想用特定的单词替换所有数字。数字有不同的格式,例如,

36
010616
010516 - 300417
01-04
2011 12

Python中是否有函数用单词替换这些类型的出现(例如,"整数"),或者正则表达式如何查找这些函数?

示例:

"This is a 10 sentence with date 010616 and intervals 06-08 200-209 01 - 09 in years 2012 26"

应该成为

"This is a NUMBER sentence with date NUMBER and intervals NUMBER NUMBER NUMBER in years NUMBER NUMBER"

2 个答案:

答案 0 :(得分:3)

使用Regex。

import re
s = "This is a 10 sentence with date 010616 and intervals 06-08 200-209 01 - 09 in years 2012 26"
print( re.sub("\d+", "NUMBER", s) )

<强>输出:

This is a NUMBER sentence with date NUMBER and intervals NUMBER-NUMBER NUMBER-NUMBER NUMBER - NUMBER in years NUMBER NUMBER

答案 1 :(得分:1)

re.sub('((?<=^)|(?<= ))[0-9- ]+(?=$| )', 'NUMBER', s)
'This is a NUMBER sentence with date NUMBER and intervals NUMBER in years NUMBER'

它的作用是:

查找带有减号和空格的数字[0-9- ]+

在匹配((?<=^)|(?<= ))

之前使用空格或字符串的开头 匹配(?=$| )

后的

和空格或字符串结尾