Regex to format 1234 to 1,234

Discussion in 'Perl Misc' started by pembed2003, Apr 27, 2004.

  1. pembed2003

    pembed2003 Guest

    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!
    pembed2003, Apr 27, 2004
    #1
    1. Advertising

  2. pembed2003 <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 27, 2004
    #2
    1. Advertising

  3. pembed2003

    pembed2003 Guest

    Tad McClellan <> wrote in message news:<>...
    > pembed2003 <> 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!
    pembed2003, Apr 28, 2004
    #3
  4. pembed2003

    Lukas Mai Guest

    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"}_" "
    Lukas Mai, Apr 28, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eddy Soeparmin

    Phone Format (770) 123-1234

    Eddy Soeparmin, Jul 28, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    3,176
    Swanand Mokashi
    Jul 28, 2003
  2. valpa
    Replies:
    11
    Views:
    1,486
    Steven D'Aprano
    Mar 24, 2009
  3. Daniel Moore
    Replies:
    19
    Views:
    307
  4. Jason Stacy
    Replies:
    2
    Views:
    125
    J├╝rgen Exner
    May 5, 2008
  5. Quarco

    ereg: ? 1234.56 ==> ? 1.234,56

    Quarco, Feb 9, 2005, in forum: Javascript
    Replies:
    3
    Views:
    153
    Quarco
    Feb 10, 2005
Loading...

Share This Page