How does this sub() function work? I expect the opposite [duplicate]

How does this sub() function work? I expect the opposite [duplicate]
python
Ethan Jackson

Why does the following Python code substitute currency characters and commas, when the code appears to me to do the opposite?

Decimal(sub(r'[^\d.]', '', '$4,987,200.04'))

If I read the regex correctly the \d. stands for all digits and periods. Why is sub is substituting all characters that are not digits and periods for '', instead of substituting the digits and periods themselves?

I.e. I expect the example string "$4,987,200.04" to end up as "$,," not "4987200.04".

Answer

If the characters inside the regular expression block [] start with a ^, it inverts the meaning so that it matches everything that is not part of the list.

Related Articles