Home > database >  Regex: Substracting characters from \D
Regex: Substracting characters from \D

Time:04-02

in a regex string, I would like to replace all non-digit characters except for ",". In python, replacing all the non-digit characters

import re
text    = "I like the numbers 15, 20 and 25"
pattern = r"\D" # or pattern = r"[\D]" or pattern = r"[^\d]"
re.sub(pattern," ",text)

but it also replaces ",". Can I subtract the "," character from \D in any way?

CodePudding user response:

So, everything except a digit(\d) or a ,?

pattern = r"[^\d,]"

CodePudding user response:

I think the following regex should work:

[^\d,]

  • Related