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.
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.
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.
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 |