问题
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