Skip to content Skip to sidebar Skip to footer

Remove Period[.] And Comma[,] From String If These Does Not Occur Between Numbers

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,.'

Solution 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

Post a Comment for "Remove Period[.] And Comma[,] From String If These Does Not Occur Between Numbers"