LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-14-2003, 10:31 AM   #1
gumby
LQ Newbie
 
Registered: May 2003
Posts: 5

Rep: Reputation: 0
Unhappy regular expression


I'm a php'er that needs to match any positive number with up to 2 decimal places using ereg. I see lots of regex examples for floating points, but none that limit the number of decimal places. Can anyone help by providing an example for this specific match?
 
Old 07-14-2003, 04:54 PM   #2
youngstorm
Member
 
Registered: May 2003
Location: Valdosta, GA, USA
Distribution: Fedora 14, Windoze XP & 7
Posts: 225

Rep: Reputation: 31
Hi,
I need more info.
Do you want to find only numbers with 2 or less decimals and not ones with more?
or, do you want to find all numbers and the ones with more than 2 decimals, chop off the extra numbers.
 
Old 07-15-2003, 08:38 AM   #3
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
Not sure how usefull my reply will be since i don't know PHP, but what you might want to try (if PHP has dynamic variable typing like Perl) is to test the value as a string and then check for the period follow by certain number of digits:
Something along these lines:
/^[^-][\d]+(.[\d]{1,2})?$/
[^-] => no minus signs
[\d]+ => match one or more digits
(.[\d]{1, 2})? => either nothing or a period followed by upto 2 digits.
 
Old 07-15-2003, 12:13 PM   #4
youngstorm
Member
 
Registered: May 2003
Location: Valdosta, GA, USA
Distribution: Fedora 14, Windoze XP & 7
Posts: 225

Rep: Reputation: 31
here is a PHP program that should do what you want.
just copy it to a file and run it at the command line
let know if you need more help
michael


#!/usr/bin/php

<?php

$varSet = array("1", "2.0", "3.1", "4.21", "5.321", "6.4321", "7.54", "-8.65", "-9.765");

for ($n = 0; $n < 9; $n++)
{
if (ereg("^[^-]+((\.)([[:digit:]]{0,2}))$", $varSet[$n]))
{
printf("%s\n", $varSet[$n]);
}
}

?>
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with regular expression aecaudel Programming 6 11-04-2005 05:28 AM
Need help with Regular Expression subaruwrx Linux - Newbie 6 09-04-2004 07:48 PM
Anyone know regular expression? ahhua Linux - Software 1 12-04-2003 08:13 AM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 10:08 AM
Regular Expression slizadel Programming 4 07-28-2003 05:16 AM


All times are GMT -5. The time now is 11:33 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration