[oclug] Quick Regexp question

psaz at rogers.com psaz at rogers.com
Mon Nov 3 10:28:16 EST 2003


If I'm not mistaken, inserting ? after (\d{3}) should cause a non-greedy match...and I believe those start from the right.

I dont have perl with me right now so I cant test it.

Pierre
> 
> From: Glenn Jackman <glennj at ncf.ca>
> Date: 2003/11/03 Mon AM 01:35:24 EST
> To: oclug at lists.oclug.on.ca
> Subject: Re: [oclug] Quick Regexp question
> 
> On 2003-11-02 23:29, Paul Faure wrote:
> > How do I convert 1234567 to 1,234,567 in perl?
> > So far I got
> >   $vol=~s/(\d{3})/,$1/g;
> > But that creates ,123,4567
> > That would be fine if it started searching from right to left instead of
> > left to right.
> 
> You need to do it in a while loop:  repeatedly, look for some digits
> followed by 3 digits, and insert a comma in between:
>     
>     sub commify {
>         my $n = shift;
>         while ($n =~ /^([-+]?\d+)(\d{3}.*)/) {$n = "$1,$2"}
>         return $n;
>     }
> 
>     $vol = commify($_);
> 
> See: perldoc -q commas
> 
> -- 
> Glenn Jackman
> NCF Sysadmin
> glennj at ncf.ca
> -- 
> OCLUG general discussion list
> OCLUG at lists.oclug.on.ca
> http://www.oclug.on.ca/mailman/listinfo/oclug
> 

1




More information about the OCLUG mailing list