1

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?

3
  • 4
    one option don't use regex to remove the commas, use replace(',','') Commented Nov 17, 2017 at 13:27
  • 2
    Just chain the .replace(',',''). A regex engine cannot perform a replacement while extracting at the same time. Commented Nov 17, 2017 at 13:28
  • Maybe you could shed some light on the reason you need this to be done in a single regex match operation - as this most likely is not possible. You can't match non-continuous content into a single group. Commented Nov 17, 2017 at 13:31

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

2

You could use split and join:

import re

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

match = re.findall(r'\$([\d,]+)', s)
number = ''.join(match[0].split(','))
print(number)

2 Comments

You don't use the fact that the number needs to be inside [], right?
For this example my provided solution works. I am just using the fact that there is a $ in front of the number. If there are other numbers with leading $ that must not be returned but only the ones additionally encased in [] then just extend the regex to: '[.*\$([\d,]+)]'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.