String filtering commas and numbers
Question: I want to filter a string in Python, to get only commas `,` and numbers
`[0-9]`.

    
    
    import re
    x="$HGHG54646JHGJH,54546654"
    m=re.sub("[^0-9]","",x)
    print(m)
    

The result is:

    
    
    5464654546654
    

instead of:

    
    
    54646,54546654
    


Answer: With your current code, you simply match `[0-9]`. Simply add a comma `,` as a
valid character, and use a backslash to escape to the literal (`\,`):

    
    
    import re
    x="$HGHG54646JHGJH,54546654"
    m=re.sub("[^0-9\,]","",x)
    print(m)
    

**Outputs:**

    
    
    54646,54546654
    

[The docs](https://docs.python.org/3/library/re.html#regular-expression-
syntax) have further information regarding other special characters that must
be escaped with a backslash to acquire the literal, such as `?` and `*`.

