Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing characters in a regex

Using Python, I have the following strings:

['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']

I need to replace each of the dots with a space, but not the periods in the numbers. So the result should look like this:

['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']

I've tried the following:

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

But, this code doesn't retain the white space. Thanks!

like image 822
Chad Larson Avatar asked Mar 14 '23 11:03

Chad Larson


1 Answers

Use negative lookarounds in re.sub

>>> 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']
like image 54
Avinash Raj Avatar answered Mar 24 '23 17:03

Avinash Raj