Replacing characters in a regex

Chad Larson

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!

Avinash Raj

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Replacing characters in a string in C++

来自分类Dev

Regex - Escape escape characters

来自分类Dev

REGEX获得“ any_characters((number,number))”

来自分类Dev

Regex: How to match sequence of SAME characters?

来自分类Dev

Check regex for letters numbers and underscore characters

来自分类Dev

Adding several special characters to existing regex

来自分类Dev

Regex pattern to find n non-space characters of x length after a certain substring

来自分类Dev

Elegant way to use regex to match order-indifferent groups of characters (in a substring) while limiting how may times a given character can appear?

来自分类Dev

Replacing NSURLConnection with NSURLSession

来自分类Dev

Replacing a letter with a word

来自分类Dev

Replacing pattern with variable

来自分类Dev

Replacing strings in lists C#

来自分类Dev

Highcharts replacing column/tooltip label

来自分类Dev

Replacing innercontent in a html using jquery

来自分类Dev

(?= regex)VS(?:regex)

来自分类Dev

Replacing EventBus with RxJava - N subscribers always listening

来自分类Dev

Replacing zero's and NA with recursive value

来自分类Dev

Replacing ListView row with another layout onClick

来自分类Dev

Find & replace in visual studio not replacing all occurrences

来自分类Dev

Replacing {phrase} with <a href="/phrase">phrase</a> in rails

来自分类Dev

Conditionally replacing column values with data.table

来自分类Dev

Replacing a space between 2 numbers(not text) with a comma

来自分类Dev

addToBackstack not working when replacing fragments in android

来自分类Dev

Html Agility Pack: replacing script tags

来自分类Dev

OWASP AntiSamy are replacing line breaks to espaces (JAVA)

来自分类Dev

RegEx调试

来自分类Dev

regex for '(number)'

来自分类Dev

Linq与Regex

来自分类Dev

REGEX NOTEPAD ++

Related 相关文章

热门标签

归档