RegexBuilder · regex answers
Regex for a dollar amount
/^\$?\d{1,3}(,\d{3})*(\.\d{2})?$|^\$?\d+(\.\d{2})?$/Two alternatives: the grouped form with thousands separators, and the plain form without them. Keeping them separate is what stops 1,23,456 from matching, which a single loose pattern would allow.
What each part does
| \$? | an optional dollar sign |
| \d{1,3}(,\d{3})* | one to three digits, then any number of comma-separated triples |
| (\.\d{2})? | an optional two-digit cent part |
Tested against real input
Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.
| Input | Result | Matched |
|---|---|---|
| $1,234.56 | matches | $1,234.56 |
| 1234.56 | matches | 1234.56 |
| $0.99 | matches | $0.99 |
| 1,000 | matches | 1,000 |
| $1,23.456 | no match | — |
| 1,2345 | no match | — |
| $1.5 | no match | — |
| $-5 | no match | — |
The mistake to avoid
Doing arithmetic on the matched text. Strip the separators and the sign first, and prefer integer cents over floating point.
Try it on your own text
Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.
Open RegexBuilderOther regex answers
- Regex for an email address
- Regex for a US phone number
- Regex for an IPv4 address
- Regex for a URL
- Regex for a date in YYYY-MM-DD format
- Regex to match digits only
- Regex for alphanumeric characters
- Regex to find special characters
- Regex for a strong password
- Regex to match whitespace
- Regex to trim leading and trailing whitespace
- Regex for a UUID