Remove decimal points and commas using regex in python
Question: I have the following string:

    
    
    st='19.000\n20,000'
    

i want to remove the commas and points **ONLY FOR NUMBERS**. I am using the
following code

    
    
    re.sub(r'[^\d\.]','',st)
    

The result is:

> '19.00020000'

I am newbie in regex. How do I preserve the new line and remove the dot Can
anyone help?


Answer: `^\d` matches everything that is not a digit.

Instead, you should use `(?<=\d)[,\.]`.

`(?<=\d)` ensures that there are digits before the comma or the point.

    
    
    import re
    
    st = '19.000\n20,000\na.a,a'
    
    print(re.sub(r'(?<=\d)[,\.]','',st))
    >> 19000
       20000
       a.a,a
    

