Home > database >  RegEx: How do I capture the dollar amount?
RegEx: How do I capture the dollar amount?

Time:11-30

I am trying to capture the dollar amount from each of these lines:

On December 31, 2018, I had $1 thousand.

I had $5 million on December 31, 2018.

The amount of money available was $5,000,000 based on the information provided.

The family, on December 31, 2018, held approximately $3 billion in cash.

I have tried using

(\$\d .\w )

but it only captures the $1 thousand, $3 million, $5,000 or $3 billion. The entire dollar amount from option 2 isn't captured.

CodePudding user response:

How about (\$\d ( \w |[\d,] ))?

https://regex101.com/r/LlnUPj/1

CodePudding user response:

The pattern (\$\d .\w ) does not fully match $5,000,000 as the dot (that can match any character, so also a comma) only matches once in the pattern and \w and \d do not match a comma.

Note that \w matches 1 word characters and can for example also match $5 test


For a match only, you don't need a capture group. You could make the pattern a bit more specific and extend it accordingly:

\$\d{1,3}(?:,\d{3})*(?:\s (?:thousand|[mb]illion))?

The pattern matches:

  • \$ Match $
  • \d{1,3}(?:,\d{3})* match 1-3 digits and optionally repeat , and 3 digits
  • (?: Non capture group
    • \s Match 1 whitespace chars
    • (?:thousand|[mb]illion) Match either thousand, million or billion
  • )? Close the non capture group and make it optional

See a regex demo.

  • Related