如果这些不在数字之间出现,则从字符串中删除句点 [.] 和逗号 [,] [英] Remove period[.] and comma[,] from string if these does not occur between numbers

查看:53
本文介绍了如果这些不在数字之间出现,则从字符串中删除句点 [.] 和逗号 [,]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅当数字之间不出现逗号和句点时,我才想从文本中删除逗号和句点.

所以,下面的文字应该返回

这件衬衫,很好看.它的成本是丹麦克朗,.1.500,00,."这件衬衫很漂亮,售价 1.500,00 丹麦克朗"

我试过

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

但它不会替代文本中的任何内容.

解决方案

你可以试试这个:

<预><代码>>>>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

这篇关于如果这些不在数字之间出现,则从字符串中删除句点 [.] 和逗号 [,]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆