替换正则表达式中的字符

时间:2016-01-27 17:28:41

标签: python regex replace substitution

使用Python,我有以下字符串:

['taxes.............................       .7        21.4    (6.2)','regulatory and other matters..................$   39.9        61.5        41.1','Producer contract reformation cost recoveries............................   DASH        26.3        28.3']

我需要用空格替换每个点,而不是数字中的句点。所以结果应该是这样的:

['taxes                                    .7        21.4    (6.2)','regulatory and other matters                  $   39.9        61.5        41.1','Producer contract reformation cost recoveries                               DASH        26.3        28.3']

我尝试过以下方法:

dots=re.compile('(\.{2,})(\s*?[\d\(\$]|\s*?DASH|\s*.)')
newlist=[]
for each in list:
    newline=dots.sub(r'\2'.replace('.',' '),each)
    newdoc.append(newline)

但是,此代码不会保留空白区域。谢谢!

2 个答案:

答案 0 :(得分:6)

re.sub

中使用negative lookarounds
>>> import re
>>> s = ['taxes.............................       .7        21.4    (6.2)','regulatory and other matters..................$   39.9        61.5        41.1','Producer contract reformation cost recoveries............................   DASH        26.3        28.3']
>>> [re.sub(r'(?<!\d)\.(?!\d)', ' ', i) for i in s]
['taxes                                    .7        21.4    (6.2)', 'regulatory and other matters                  $   39.9        61.5        41.1', 'Producer contract reformation cost recoveries                               DASH        26.3        28.3']

答案 1 :(得分:1)

如果输入始终与您的样本一样,您也可以使用非word boundary

\.\B替换为一个空格

这仅检查一段时间后是否没有单词字符。因此它会匹配0.但不匹配0.0

See demo at regex101