CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
RE for number with or w/o comma

 



ejaggers1
Novice

Jun 22, 2010, 7:59 AM

Post #1 of 4 (2817 views)
RE for number with or w/o comma Can't Post

How can you match a number string that may or may not have commas? i.e 123 or 1,234 or 123,456,789...


rovf
Veteran

Jun 23, 2010, 2:55 AM

Post #2 of 4 (2795 views)
Re: [ejaggers1] RE for number with or w/o comma [In reply to] Can't Post

Using the ?-Quantifier.

From perlre:


Quantifiers

The following standard quantifiers are recognized:

* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times



Zhris
User

Jun 23, 2010, 9:09 PM

Post #3 of 4 (2745 views)
Re: [ejaggers1] RE for number with or w/o comma [In reply to] Can't Post

A basic expression which would allow commas to occur anywhere in the number string:

m/^[0-9,]+$/

Or an untested expression which would allow a comma after the first number or not, then after every 3 numbers or not, although it would allow i.e. "1,234,56789,444":

m/^\d\,?(\d{1,2}|\d{3}\,?)*$/

Sometimes I wonder if it would be easier to validate that the string contains just numbers or commas, remove all commas, then use sprintf to replace the commas in their proper places.

Chris


(This post was edited by Zhris on Jul 25, 2010, 6:31 AM)


ejaggers1
Novice

Jun 24, 2010, 5:34 AM

Post #4 of 4 (2711 views)
Re: [Zhris] RE for number with or w/o comma [In reply to] Can't Post

Thanks Chris.....That was a BIG help!!!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives