PatternRegex

Match numbers with thousand separators

Pull out numbers formatted with comma thousand separators, like 1,000,000, from reports, invoices, or scraped text.

Last updated

Patterng
\b\d{1,3}(?:,\d{3})+\b
Open in Regex Tester & Builder

Matches

  • The budget is 1,000,000 dollars
  • Population: 12,345 people

Doesn’t match

  • The count was 1000000 today
  • The code is 12,34 not thousands

Why it’s written this way

\d{1,3} matches the leading one-to-three digits, and the non-capturing group (?:,\d{3})+ then requires one or more repeats of a comma followed by exactly three digits, mirroring the Western convention of grouping from the right.

Word boundaries on both ends stop the match from starting or ending mid-run, so it won't grab just the tail of a longer plain number that happens to sit nearby.

Edge cases to know

  • →It assumes Western thousands grouping. Indian-style lakh formatting like 1,00,000 breaks the three-digit assumption, and the pattern still latches onto the trailing 00,000 as if it were a valid group — a genuine false positive worth knowing about.
  • →A plain unformatted number such as 1000000 is correctly ignored, but that also means the pattern can't help you find numbers that are missing their separators.

Part by part

\b\d{1,3}
The leading group: one to three digits before the first separator, which is what 1, 12 and 999 all look like at the front of a formatted number.
(?:,\d{3})+
One or more groups of a comma followed by exactly three digits. The + requires at least one separator, so a plain number does not match.
\b
Boundaries at both ends, keeping the match from starting or finishing inside a longer digit run.

Common mistakes

  • [\d,]+

    A flat class accepts commas anywhere, so "1,,2" and ",,," match. Every rule that makes a thousands separator meaningful is about position, and a character class cannot express position.

    \b\d{1,3}(?:,\d{3})+\b
  • \b\d{1,3}(?:,\d{3})*\b

    A star makes the separator optional, so every ordinary number matches too. If you want both forms that is fine, but then the pattern no longer identifies formatted numbers specifically.

  • \b\d+(?:,\d{3})+\b

    An unbounded leading group accepts 12345,678, where the first section is already longer than a thousands group. It matches malformed output that a stricter pattern would flag.

Questions

Does it handle European formatting?

No. Much of Europe uses the dot as a thousands separator and the comma as a decimal point, so 1.234,56 means what 1,234.56 means elsewhere. Swap the two characters for those locales — the structure is identical.

Does it match the decimal part?

No, it stops at the last thousands group. Scanning "1,234.56" returns 1,234 and leaves the decimals. Append (?:\.\d+)? if you need the whole amount.

What about the Indian numbering system?

It groups differently — 12,34,567 rather than 1,234,567 — so this pattern matches only part of such a number. That system needs its own pattern, since the group sizes vary by position.

How do I convert a match into a number?

Strip every comma and parse the result. Do not rely on a locale-aware parser unless you know the input's locale, because the same string means different values in different conventions.

Related in Patterns