-2

I'm trying to account for the possibility of any USD denomination and I came up with this:

\$\d+\.?\,?\d+\.?\d+

This works for entries like $10,500.23, $1,050.23, $105.23, $105, $10, $1

But won't work for things like $.23

I tried using \d+? instead of just \d? but that doesn't seem to work either (maybe there is a special way of handling this that I'm unaware of?)

Yannis
  • 1,682
  • 7
  • 27
  • 45
David
  • 459
  • 5
  • 13

1 Answers1

0

The + symbol means one or more, whereas the ? symbol means 0 or 1. If you add an OR statement (|) you can have it work with either, making the completed (and fully functional) statement \$\d+|\d?\.?\,?\d+\.?\d+

one potential issue with this is that the regex doesn't catch items with only one digit after the ., not sure if that is an issue for you in your implementation or not.

Zeke Egherman
  • 359
  • 5
  • 17