Search code examples
pythonregexstringdigits

Regex Python extract digits between two strings in a single expression


I want only digits existing between two characters in order to get an integer dollar value, e.g. from string:

"Advance [Extra Value of $1,730,555] in packages 2,3, and 5."

we want to obtain "1730555".

We can use \$(.*)\] to get "1,730,555", but how do we remove the commas in the same expression, while retaining the possibility of arbitrary many commas, ideally getting the number in a single capturing group?


Solution

  • You can try like this

    import re
    text = "Advance [Extra Value of $1,730,555] in packages 2,3, and 5."
    match = re.findall(r'\$(.*)]',text)[0].replace(',','')
    print match