Scherf Scherf - 7 months ago 10
Python Question

Regex: excluding results based on non capture group?

So I'm trying to use the following regex to match patterns of valid prices

^\$[1-9]\d{0,2}(?:\,\d{3})?(?:\.\d{2})?


(derived from another question here on SO)

Constraints for matching being:


  1. $1.00 <= price <= $999,999.99

  2. Decimal place is not required ($1 is valid as is $1.00)

  3. Single digit post decimal place is invalid ($1.1, $2.9 etc..)

  4. 3+ digits post decimal place is invalid ($1.111, $2.999 etc..)



The above pattern works to deal with the first two constraints but not the others. The issue being that in the case of (3) or (4), the portion of the price that precedes the decimal point is matched.

Ex:

$100,000.4 matches to $100,000
$200,000.444 matches to $200,000.44


How can I modify this so that in the case of (3) or (4) the entire string is not matched?

ccf ccf
Answer

You can use Negative Lookahead (?![\d.,]) to assert there is no digit, comma or dot following matched price:

\$[1-9]\d{0,2}(?:,\d{3})?(?:\.\d{2})?(?![\d.,])

Regex 101 demo.