## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# 6.8. Numbers with Thousand Separators

## Problem

You want to match numbers that use the comma as the thousand separator and the dot as the decimal separator.

## Solution

Mandatory integer and fraction:

`^[0-9]{1,3}(,[0-9]{3})*\.[0-9]+\$`
 Regex options: None Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

Mandatory integer and optional fraction. Decimal dot must be omitted if the fraction is omitted.

`^[0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)?\$`
 Regex options: None Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

Optional integer and optional fraction. Decimal dot must be omitted if the fraction is omitted.

`^([0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)?|\.[0-9]+)\$`
 Regex options: None Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

The preceding regex, edited to find the number in a larger body of text:

`\b[0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)?\b|\.[0-9]+\b`
 Regex options: None Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

## Discussion

Since these are all regular expressions for matching floating-point numbers, they use the same techniques as the previous recipe. The only difference is that instead of simply matching the integer part with `[0-9]+`, we now use `[0-9]{1,3}(,[0-9]{3})*`. This regular expression matches between 1 and 3 digits, followed by zero or more groups that consist of a comma and 3 digits.

We cannot use `[0-9]{0,3}(,[0-9]{3})*` to make the integer part optional, because that would match numbers with a leading comma, e.g., `,123`. It’s the same trap of making ...

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required