6.12. Add Thousand Separators to Numbers

Problem

You want to add commas as the thousand separator to numbers with four or more digits. You want to do this both for individual numbers and for any numbers in a string or file.

For example, you’d like to convert this:

There are more than 7000000000 people in the world today.

To this:

There are more than 7,000,000,000 people in the world today.

Tip

Not all countries and written languages use the same character as the thousand separator. The solutions here use a comma, but some people use dots, underscores, apostrophes, or spaces for the same purpose. If you want, you can replace the commas in this recipe’s replacement strings with one of these other characters.

Solution

The following solutions work both for individual numbers and for all numbers in a given string. They’re designed to be used in a search-and-replace for all matches.

Basic solution

Regular expression:

[0-9](?=(?:[0-9]{3})+(?![0-9]))
Regex options: None
Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

Although this regular expression works equally well with all of the flavors covered by this book, the accompanying replacement text is decidedly less portable.

Replacement:

$&,
Replacement text flavors: .NET, JavaScript, Perl
$0,
Replacement text flavors: .NET, Java, XRegExp, PHP
\0,
Replacement text flavors: PHP, Ruby
\&,
Replacement text flavor: Ruby
\g<0>,
Replacement text flavor: Python

These replacement strings all put the matched number back using backreference zero (the entire ...

Get Regular Expressions Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.