RRegexBuilder
Get RegexBuilder

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.

InputResultMatched
$1,234.56matches$1,234.56
1234.56matches1234.56
$0.99matches$0.99
1,000matches1,000
$1,23.456no match
1,2345no match
$1.5no match
$-5no 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 RegexBuilder

Other regex answers