且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果这些不在数字之间出现,则从字符串中删除句点 [.] 和逗号 [,]

更新时间:2022-12-28 10:55:45

你可以试试这个:

>>>s = "这件衬衫,很好看.售价 DKK,.1.500,00,.">>>re.sub('(?<=\D)[.,]|[.,](?=\D)', '', s)'这件衬衫很漂亮,售价 1.500,00 丹麦克朗'

使用正向后视断言检查符号前面是否有非数字字符,并使用正向前瞻断言对同一字符集进行替代 检查它后面是否有一个非数字字符.

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

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.

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