Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regex to format 1234 to 1,234

Reply
Thread Tools

Regex to format 1234 to 1,234

 
 
pembed2003
Guest
Posts: n/a
 
      04-27-2004
Hi all,
I am trying to come up with a regex to format number like:

1234
-1234
1234567

to:

1,234
-1,234
1,234,567

The numbers will always be interger. So far, I have the following:

#1:

while(<>){
chomp;
$_ = reverse split //;
s/(\d{3})\B/$1,/g;
$_ = reverse split //;
print "$_\n";
}

#2:

while(<>){
chomp;
my $n = $_ =~ s/^-// ? '-' : '';
$_ = 'x' . $_ while(length($_) % 3);
s/(.{3})\B/$1,/g;
s/^[x,]+//;
print "$n$_\n";
}

#3:

while(<>){
chomp;
my $n = '';
while(s/\B(\d{3})$//g){
$n = ",$1" . $n;
}
$n = $_ . $n if length;
print "$n\n";
}

All three methods seems to be a bit too "long", is there a better way?
I am trying to do this as a way for me to learn regex so I would like
not to use a module if possible. Thanks!
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      04-27-2004
pembed2003 <(E-Mail Removed)> wrote:

> I am trying to come up with a regex to format number like:


> 1,234
> -1,234
> 1,234,567



You are expected to check the Perl FAQ *before* posting to
the Perl newsgroup you know...


perldoc -q commas

How can I output my numbers with commas added?


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
pembed2003
Guest
Posts: n/a
 
      04-28-2004
Tad McClellan <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>.. .
> pembed2003 <(E-Mail Removed)> wrote:
>
> > I am trying to come up with a regex to format number like:

>
> > 1,234
> > -1,234
> > 1,234,567

>
>
> You are expected to check the Perl FAQ *before* posting to
> the Perl newsgroup you know...
>
>
> perldoc -q commas
>
> How can I output my numbers with commas added?


perldoc -a 'got it'

Thanks!
 
Reply With Quote
 
Lukas Mai
Guest
Posts: n/a
 
      04-28-2004
Purl Gurl schrob:
[...]
> Although not all that efficient, beneath my signature

^^^^^^^^^^^^^^^^^^^^
> is a sample of fun code from way back.

[...]

Isn't that impossible? AFAIK a signature is defined as m{^-- \n.*\z}sm,
so "beneath my signature" means "after the end of the article".

Confused, Lukas
--
use warnings; use strict;
BEGIN{*'_ =sub {print@_}}_"l";END {_"h";INIT {_"P";CHECK{_" ";BEGIN{_"s";
CHECK{_"n";INIT{_"r";END{_"k";BEGIN{_"J";CHECK{_"t ";END {_"r";INIT {_"e"}
_", "}_"h"}_"u"}_"e"}_" "}_"o"}_"t" }_"a" }_"e";END {_"c"}_"r" }_"a"}_" "
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is the expression 1 ? "123" : "1234" a valid one ? 邓尧 C Programming 6 04-25-2012 02:11 AM
David Mark's Javascript Tip Du Jour - Volume #1 - Tip #1234 - How toMeasure Element Dimensions David Mark Javascript 58 12-06-2011 10:13 PM
ereg: ? 1234.56 ==> ? 1.234,56 Quarco Javascript 3 02-10-2005 09:03 AM
Phone Format (770) 123-1234 Eddy Soeparmin ASP .Net 3 07-28-2003 08:03 PM
Fire IISReset command on event aspnet_wp.exe (PID: 1234) was recycled... Sunil Menon ASP .Net 2 07-27-2003 04:47 AM



Advertisments