I want to convert 2333444 to 2,333,444, but I'm not clear on how to have the expression work its way from the right to the left as opposed to the other way around. I'm writing Perl, but any regEx syntax is fine, I'll convert it if you are more comfortable with java or javascript.
Asked
Active
Viewed 2,057 times
2
-
6Is there a specific reason you want to use regular expressions to accomplish this? – meandmycode Aug 02 '09 at 22:04
-
2duplicate of: http://stackoverflow.com/questions/721304/insert-commas-into-number-string – sth Aug 02 '09 at 22:07
-
1Use Bill or Brian's answers of applying the standard number formatting library, rather than attempting to re-invent a perfectly good wheel. – Peter Boughton Aug 02 '09 at 22:12
-
1I believe regEx IS a perfectly good wheel, so. I'm not clear on how using it for something as straight ahead as this is inferior to importing another module AND limiting myself to its use (rather than allowing the client to do the same work, in javascript, for example). – Yevgeny Simkin Aug 03 '09 at 23:05
-
I don't want this to sound like flamebait, but the regexp can't be *that* straightforward for the very obvious reason that you're asking about it. The reason that I (and others) have suggested you use a library is that someone's gone through the hard work of providing a proper solution with all edge cases and locale-specifics resolved. Very often people will ask for a solution using a particular tool (in this case, regexp), not realising that it's not the right tool. If you really want to avoid such suggestions, then I'd be explicit in your question as to what you won't accept. – Brian Agnew Aug 03 '09 at 23:28
-
1Brian, I'm certainly not going to flame, but I think you're being unfair. A) as the selected solution demonstrates, it is, in fact, JUST that easy (I just didn't know how to properly do negative look aheads) and B) my question explicitly requested THIS solution, and not alternative ones because, as it happens, I know enough regEx to know that it's exactly the tool for this kind of problem. Furthermore, even though some of the answers provided weren't on topic, they're still quite useful. Someone else might be delighted to learn of the format_number lib. – Yevgeny Simkin Aug 06 '09 at 09:58
-
That someone would arguably be a better developer ;) – meandmycode Aug 06 '09 at 10:40
-
1pray tell, what would be the argument here, against using regEx to do EXACTLY what it was designed to, and excels at, doing? Consider further that moments after I posted this, I realized that this portion of the processing makes a great deal more sense on the client... porting it to javascript took exactly 15 seconds. – Yevgeny Simkin Aug 06 '09 at 16:40
4 Answers
4
Check out Number::Format. This module provides a wide variety of number formatting solutions. e.g. for what you require
format_number(1234567.89, 2)
yields '1,234,567.89'
Don't forget that what you're trying to do is locale-specific, and this module will handle that for you. A simple regexp solution won't do that on it's own.

Brian Agnew
- 268,207
- 37
- 334
- 440
-
1Not sure why everyone is marking this (and the next) answer up? This is not an answer to my question. It's an alternative solution, which, as it happens, doesn't work for me since I am working as much on the client as on the server. Since when is regEx "reinventing the wheel". I appreciate the fact that there's a library that does this in perl, but that's not what I was asking. – Yevgeny Simkin Aug 03 '09 at 23:03
3
You want to use the Number:Format extension, not a regular expression. You'll want to specify THOUSANDS_SEP= ','
.

Bill the Lizard
- 398,270
- 210
- 566
- 880
2
s/(?<=\d)(?=(\d\d\d)+(?!\d))/,/g
this will do it in perl style regexs

ennuikiller
- 46,381
- 14
- 112
- 137