Finance
US dollar amount Regex
This pattern matches a US dollar amount written with a leading dollar sign. Thousands may be grouped with commas in groups of three, or the number can be written without grouping, and an optional two digit cents part follows a decimal point. A leading minus sign is allowed for refunds and adjustments. It works well for validating price fields, cleaning scraped product listings and checking spreadsheet exports before import. It does not accept amounts written with a trailing USD label, accounting parentheses, or a single cents digit such as $12.5, so normalise those formats first if your source uses them.
Pattern (JavaScript)
/^-?\$(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?$/Starts at the start of the line/string, then the character "-", optionally (zero or one time), then the character "$", then (a digit (0-9), between 1 and 3 times, then (the character ",", then exactly 3 digits), zero or more times OR one or more digits), then (the character ".", then exactly 2 digits), optionally (zero or one time), then the end of the line/string.
Examples
Matches
- ✓ $1,234.56
- ✓ $0.99
- ✓ $1000
- ✓ -$45.00
Does not match
- ✗ $1,23.45
- ✗ 1234.56
- ✗ $12.5
- ✗ $ 12
Try it
0 matches (JavaScript engine)
$1,234.56
$0.99
$1000
-$45.00
$1,23.45
1234.56
$12.5
$ 12Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^-?\$(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?$/g;
const text = "$1,234.56";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^-?\$(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?$')
text = '$1,234.56'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^-?\$(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?$/';
$text = '$1,234.56';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^-?\\$(?:\\d{1,3}(?:,\\d{3})*|\\d+)(?:\\.\\d{2})?$");
String text = "$1,234.56";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^-?\$(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?$`)
text := "$1,234.56"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^-?\$(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?$");
var text = "$1,234.56";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^-?\$(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?$").unwrap();
let text = "$1,234.56";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^-?\$(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?$/
text = '$1,234.56'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why is $12.5 rejected?
- Prices in US dollars are conventionally written with exactly two cents digits. Requiring two digits catches truncated values and typos. Change \.\d{2} to \.\d{1,2} if your data legitimately drops the trailing zero.
- Does it allow amounts without commas?
- Yes. The pattern accepts either correctly grouped commas or a plain run of digits, so $1000 and $1,000 both match. What it rejects is badly grouped commas such as $1,23.
- How do I extract dollar amounts from a sentence?
- Remove the ^ and $ anchors, add the g flag, and wrap the end with a word boundary check. That lets the pattern find every amount inside a longer piece of text instead of testing a whole field.
Related patterns
- Euro amount^(?:€ ?(?:\d{1,3}(?:[.,]\d{3})*|\d+)(?:[.,]\d{2})?|(?:\d{1,3}(?:[. ]\d{3})*|\d+)(?:,\d{2})? ?€)$
- Common ISO 4217 currency code^(?:USD|EUR|GBP|JPY|CHF|CAD|AUD|NZD|CNY|HKD|SGD|INR|AED|SAR|SEK|NOK|DKK|ZAR|BRL|MXN)$
- Accounting negative amount in parentheses^\((?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?\)$
- American Express card number shape^3[47]\d{2}[ -]?\d{6}[ -]?\d{5}$