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.
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.
|Regex options: None|
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 text flavors: .NET, Java, XRegExp, PHP|
|Replacement text flavors: PHP, Ruby|
|Replacement text flavor: Ruby|
|Replacement text flavor: Python|
These replacement strings all put the matched number back using backreference zero (the entire ...