Help with a regular expression for 0,1 or 2 decimal places
Help with a regular expression for 0,1 or 2 decimal places
am 24.10.2005 22:59:24 von shaun thornburgh
Hi,
I am trying to create a regular expression for a width of a room, the value
can be a whole integer (up to 999) with up to 2 decimal places -when it is
stored in the database mysql will pad the value accordingly.
/^[0-9]{1,3}.?[0-9]{0,2}?$/
The only problem I have found with above is that I can enter 1.
Is it possible to modify it so that I can only enter the decimal point if I
enter at least digit after it?
Also is there any difference between [0-9] and \d
Thanks for your help
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Help with a regular expression for 0,1 or 2 decimal places
am 25.10.2005 02:24:20 von Al
Shaun wrote:
> Hi,
>
> I am trying to create a regular expression for a width of a room, the value
> can be a whole integer (up to 999) with up to 2 decimal places -when it is
> stored in the database mysql will pad the value accordingly.
>
> /^[0-9]{1,3}.?[0-9]{0,2}?$/
>
> The only problem I have found with above is that I can enter 1.
>
> Is it possible to modify it so that I can only enter the decimal point if I
> enter at least digit after it?
>
> Also is there any difference between [0-9] and \d
>
> Thanks for your help
Try this..
^\d{1,3}($|\.\d{1,2}$)
1,2 or 3 digits followed by end OR decimal point and 1 or 2 digits and end of line.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Help with a regular expression for 0,
am 25.10.2005 04:08:37 von Richard Lynch
On Mon, October 24, 2005 3:59 pm, Shaun wrote:
> I am trying to create a regular expression for a width of a room, the
> value
> can be a whole integer (up to 999) with up to 2 decimal places -when
> it is
> stored in the database mysql will pad the value accordingly.
>
> /^[0-9]{1,3}.?[0-9]{0,2}?$/
Because '.' is special to preg, this expression ALSO matches:
12a25
5z43
7a
[aside]
Engineer: 7A, please.
Davey: What number, please?
Band: 7A!
Davey: All right, okay, just cuz I'm short
[/aside]
This might (or might not) do it:
/^[0-9]{1,3}(\\.[0-9]{1,2})?$/
The \\ is there so that PHP will turn it into \, and then you have \.
in the regular expression which the PRCE will interpret as "the
literal character known as 'dot'" instead of "any old character at
all"
> Also is there any difference between [0-9] and \d
Try it and see.
--
Like Music?
http://l-i-e.com/artists.htm
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Help with a regular expression for 0,1 or 2 decimal places
am 25.10.2005 20:55:34 von Phillip Oertel
hi,
i'm not a regex guru myself, but the following regex should work - tabke
a look at my regex test setup ...
$test = array(
"1",
"1.",
"1.2",
"1.23",
"1.234",
"1234",
"1234.",
"1234.5",
"1234.56",
"1234.567"
);
// if there's a dot, we want at least one number after it.
$regex = '/^\d+(.\d{1,2})?$/';
// test all or numbers ...
foreach ($test as $number) {
if (preg_match($regex, $number)) {
echo "ok ";
} else {
echo "no ";
}
echo $number . "
\n";
}
?>
i don't think there's a difference between \d and [0-9], but again, i am
no expert ...
regards,
phillip
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: Help with a regular expression for 0,1 or 2 decimal places
am 25.10.2005 21:19:12 von Florent Monnier
Phillip Oertel a =E9crit :
> hi,
>
> i'm not a regex guru myself, but the following regex should work - tabke
> a look at my regex test setup ...
>
>
>
> $test =3D array(
> "1",
> "1.",
> "1.2",
> "1.23",
> "1.234",
>
> "1234",
> "1234.",
> "1234.5",
> "1234.56",
> "1234.567"
> );
>
>
> // if there's a dot, we want at least one number after it.
> $regex =3D '/^\d+(.\d{1,2})?$/';
It would be better to escape the point, cause the point greps for everythin=
g.
$regex =3D '/^\d+(\.\d{1,2})?$/';
Without escaping the point, it could match for '1234:567' or something else.
> // test all or numbers ...
> foreach ($test as $number) {
> if (preg_match($regex, $number)) {
> echo "ok ";
> } else {
> echo "no ";
> }
> echo $number . "
\n";
> }
>
> ?>
>
> i don't think there's a difference between \d and [0-9], but again, i am
> no expert ...
>
> regards,
> phillip
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: Help with a regular expression for 0,1 or 2 decimal
am 25.10.2005 21:24:24 von John Nichel
Phillip Oertel wrote:
> hi,
>
> i'm not a regex guru myself, but the following regex should work - tabke
> a look at my regex test setup ...
>
>
>
> $test = array(
> "1",
> "1.",
> "1.2",
> "1.23",
> "1.234",
>
> "1234",
> "1234.",
> "1234.5",
> "1234.56",
> "1234.567"
> );
>
>
> // if there's a dot, we want at least one number after it.
> $regex = '/^\d+(.\d{1,2})?$/';
Escape the decimal point.
$regex = '/^\d+(\.\d{1,2})?$/';
--
John C. Nichel
ÜberGeek
KegWorks.com
716.856.9675
john@kegworks.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php