Remove period[.] and comma[,] from string if these does not occur between numbers

↘锁芯ラ 提交于 2021-02-10 06:16:06

问题


I want to remove comma and period from a text only when these does not occur between numbers.

So, following text should return

"This shirt, is very nice. It costs DKK ,.1.500,00,."

"This shirt is very nice It costs DKK 1.500,00"

I tried with

text = re.sub("(?<=[a-z])([[$],.]+)", " ", text) 

but it does not substitute anything in the text.


回答1:


You could try this:

>>> s = "This shirt, is very nice. It costs DKK ,.1.500,00,."
>>> re.sub('(?<=\D)[.,]|[.,](?=\D)', '', s)
'This shirt is very nice It costs DKK 1.500,00'

Using a positive lookbehind assertion to check the symbols are preceded by a non digit character, and an alternation on the same character set using a positive lookahead assertion to check it is followed by a non digit character.

https://regex101.com/r/54STMM/4



来源:https://stackoverflow.com/questions/40445290/remove-period-and-comma-from-string-if-these-does-not-occur-between-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!