Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-14-2003, 10:31 AM   #1
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
Registered: May 2003
Location: USA
Distribution: Fedora 21, RHEL 5,6&7, Windoze 7
Posts: 235

Rep: Reputation: 31
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
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:
[^-] => 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
Registered: May 2003
Location: USA
Distribution: Fedora 21, RHEL 5,6&7, Windoze 7
Posts: 235

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



$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]);



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

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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:20 AM.

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