humblefoolish humblefoolish - 27 days ago 5
Python Question

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

"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.

Answer

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

Comments