1

I'm trying to write a regex to search for a dollar amount that is $1000.00 or more and has a - in front. Also the $ should be optional.

This is what I have so far and it is not working as I expected :(

\-\$?(((\d{1,3},)+\d{3})|\d+)\.\d{2}

But it got triggered on a value -$73.75 when this value should have been ignored. Any pointers/ideas would be greatly appreciated :)

Drakkainen
  • 1,142
  • 11
  • 25

3 Answers3

4

You can simply check whether the value before . is at least 4 digits:

-\$?\d{4,}\.\d{2}

Note that, you don't need to escape the -, it's not a meta-character in regex(outside the character class).

-        # Match a hyphen
\$?      # An optional $ sign
\d{4,}   # 4 or more digits
\.       # A dot
\d{2}    # 2 digits after dot

If there can be separators (,) in your digits, then you can use this:

-\$?[\d,]*\d,\d{3}\.\d{2}

This will ensure a digit followed by comma, and 3 digits. And before that, it is quite loose in allowing any number of digits and commas.

This is of course not a very strict regex though. But again, creating one will be difficult. You should better avoid regex to validate the amount based on localization, formatting, and some minimum amount.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
3

How about not using regexes :-

>>> abs(float('-$1000'.translate(None, '$,'))) >= 1000
True
>>> abs(float('-$1,000'.translate(None, '$,'))) >= 1000
True
>>> abs(float('-$73.3'.translate(None, '$,'))) >= 1000
False
Himanshu
  • 2,384
  • 2
  • 24
  • 42
2

Given that you have commas in the match string, I'll assume you are looking at matching either strings like $1000.00 or $1,000.00.

This should capture both and you can see it in practise at rubular.

(\-?\$?(?:(?:\d{1,3}(?:,+\d{3}){1,})|\d{4,})\.\d{2})

Breaking this down:

(                     - Capture group
  -?                  - Optional hyphen
  \$?                 - Optional dollar sign
  (?:                 - Non-capture group
    (?:               - Non-capture group
      \d{1,3}         - Between 1 and 3 digits
      (?:,+\d{3}){1,} - At least 1 comma with 3 digits, repeated
    )
    |                 - OR
    \d{4,}            - At least 4 digits
  ) 
  \.                  - a literal dot
  \d{2}               - 2 digits
)

However, this will only work for Engligh/US style currency formats. In Europe the dot is the thousands indicator and comma the decimal, like so: $1.000.000,00. However, I recently answered a question that dealt with a very similar issue with currency matching in strings.

Community
  • 1
  • 1