Scherf Scherf - 2 years ago 83
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


(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.


$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 Source

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


Regex 101 demo.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download