Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
FIRST
How to add commas to the following number "1234567" as "1,234,567"?
using regex substitution and split (if possible)
SECOND
What is the difference between *? and *+ ?
Please explain with examples

Thanks in Advance!!!

Replies are listed 'Best First'.
Re: Regexp doubts
by ikegami (Patriarch) on May 01, 2010 at 05:52 UTC

    Update: Tested. Fixed. I knew I needed that \z.
    Update: Added third solution, a simplification of the second.

Re: Regexp doubts
by 7stud (Deacon) on May 01, 2010 at 06:31 UTC
    Because doing it by hand, even by an expert, is so error prone (not to mention completely illegible):
    use strict; use warnings; use 5.010; use Number::Format; my $number = 1222333444.01234567; my $formatter = new Number::Format( -decimal_digits => '4', -decimal_point => '!', ); say $formatter->format_number($number); --output:-- 1,222,333,444!0123
    Or, copy the solution from perlfaq.
Re: Regexp doubts
by toolic (Bishop) on May 01, 2010 at 13:55 UTC
Re: Regexp doubts
by Anonymous Monk on May 01, 2010 at 05:28 UTC
      Perhaps AM meant for the third link to read: Re: How do I add commas to a number?

      As to your second question, this seconds AM's first two reading recommendations. You're looking to learn the difference between "lazy quantifiers" and "possessive quantifiers." If it's easier for you, try reading "Mastering Regular Expressions, Friedl (O'Reilly)" or the "Regular Expressions Pocket Reference, Stubblebine (O'Reilly)"

Re: Regexp doubts
by johngg (Canon) on May 01, 2010 at 23:04 UTC

    There are some interesting recipes in this thread.

    Cheers,

    JohnGG