Currency amounts with commas
A regex exercise from Regexdrill. Solve it with a pattern box with live highlighting in your browser, checked against hidden test strings, and it is checked the moment you submit.
The task
The whole line must match. Match a dollar amount: $, then 0 or a number without leading zeros that uses a comma every three digits (so $1,234, never $1234), then optionally a period and exactly two digits of cents.
What this exercise teaches
\$(?:0|[1-9]\d{0,2}(?:,\d{3})*)(?:\.\d{2})? enforces the comma groups and the two-digit cents.
A hint if you get stuck
One to three digits first, then any number of groups made of a comma and exactly three digits.
This exercise is part of Pro
Pro unlocks all 52 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.
You create a free account first, so Pro is attached to it.
Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.