Re: [PHP] regular expression for integer range

2005-09-08 Thread Mark Rees
""Murray @ PlanetThoughtful"" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> > Hi all,
> >
> >
> > I want to write regular expression for checking the string format
entered
> > by user.
> >
> > the allowed formats are
> >
> > examples:
> > 10
> > 10,
> > 10,12-10
> > 12-10
> >
> > that is the valid strings are:
> > 1. only integer
> > 2. an integer, range of integers example 3
> >
> > and no other characters must be allowed.

You could simplify the matter by replacing all "-" and "," with, say, 0 and
then using the simple "\d+" (I think) regexp.

>
> Hi babu,
>
> As you've pointed out, you have 4 distinct scenarios to deal with, and it
> may be very difficult, without a great deal of tuning and tweaking, to
> define a regular expression that can effectively match all 4 scenarios
> without including false matches as well.
>
> One way of dealing with this is to build more specialized and exact
regular
> expressions for each possible scenario and group them together in an if
> statement.
>
> Eg.
>
> if (preg_match('/^\d+$/',$subject) || preg_match('/^\d+,$/',$subject) ||
> preg_match('/^\d+,\d+-\d+$/', $subject) || ){
>
> // code for successful match of valid data in $subject
>
> } else {
>
> // code for invalid data in $subject
>
> }
>
> Basically, the if/else statement is testing each distinct possible pattern
> and executing code if any of those distinct possible patterns match.
>
> It may not ultimately be the most graceful way of dealing with the
> situation, but having spent many hours attempting to tweak complex regular
> expressions looking for the magic combination, I've learned that breaking
> scenarios down this way can save a lot of development time and
frustration.
> This doesn't mean there isn't a benefit to finding the perfect regular
> expression for your needs, just that it can often be difficult to
guarantee
> your code won't be plagued by false matches or false exclusions as your
> expression becomes more and more complex.
>
> Hope this helps a little.
>
> Much warmth,
>
> Murray
> ---
> "Lost in thought..."
> http://www.planetthoughtful.org

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] regular expression for integer range

2005-09-07 Thread Murray @ PlanetThoughtful
> Hi all,
> 
> 
> I want to write regular expression for checking the string format entered
> by user.
> 
> the allowed formats are
> 
> examples:
> 10
> 10,
> 10,12-10
> 12-10
> 
> that is the valid strings are:
> 1. only integer
> 2. an integer, range of integers example 3
> 
> and no other characters must be allowed.

Hi babu,

As you've pointed out, you have 4 distinct scenarios to deal with, and it
may be very difficult, without a great deal of tuning and tweaking, to
define a regular expression that can effectively match all 4 scenarios
without including false matches as well.

One way of dealing with this is to build more specialized and exact regular
expressions for each possible scenario and group them together in an if
statement.

Eg.

if (preg_match('/^\d+$/',$subject) || preg_match('/^\d+,$/',$subject) ||
preg_match('/^\d+,\d+-\d+$/', $subject) || ){

// code for successful match of valid data in $subject

} else {

// code for invalid data in $subject

}

Basically, the if/else statement is testing each distinct possible pattern
and executing code if any of those distinct possible patterns match.

It may not ultimately be the most graceful way of dealing with the
situation, but having spent many hours attempting to tweak complex regular
expressions looking for the magic combination, I've learned that breaking
scenarios down this way can save a lot of development time and frustration.
This doesn't mean there isn't a benefit to finding the perfect regular
expression for your needs, just that it can often be difficult to guarantee
your code won't be plagued by false matches or false exclusions as your
expression becomes more and more complex.

Hope this helps a little.

Much warmth,

Murray
---
"Lost in thought..."
http://www.planetthoughtful.org

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] regular expression for integer range

2005-09-07 Thread Robin Vickery
On 9/6/05, babu <[EMAIL PROTECTED]> wrote:
> Hi all,
> 
> 
> I want to write regular expression for checking the string format entered by 
> user.
> 
> the allowed formats are
> 
> examples:
> 10
> 10,
> 10,12-10
> 12-10
> 
> that is the valid strings are:
> 1. only integer
> 2. an integer, range of integers example 3
> 
> and no other characters must be allowed.

$re = '/^(\d+)(,\d+-\d+)?$/';
preg_match($re, $value);

This only allows
 * an integer or
 * an integer followed by a comma and a range of integers

It doesn't allow anything else - even whitespace after the comma.

 -robin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] regular expression for integer range

2005-09-06 Thread John Nichel

babu wrote:

Hi all,
 
 
I want to write regular expression for checking the string format entered by user.
 
the allowed formats are
 
examples:

10
10,
10,12-10
12-10
 
that is the valid strings are:

1. only integer
2. an integer, range of integers example 3
 
and no other characters must be allowed.


Crude, but it will work

preg_match ( "/^[-0-9,]+$/", $value )

It will also match just dashes and/or just comma's with no numbers.

http://us2.php.net/manual/en/reference.pcre.pattern.syntax.php

--
John C. Nichel
ÜberGeek
KegWorks.com
716.856.9675
[EMAIL PROTECTED]

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] regular expression for integer range

2005-09-06 Thread Philip Hallstrom




On Tue, 6 Sep 2005, babu wrote:


Hi all,


I want to write regular expression for checking the string format entered by 
user.

the allowed formats are

examples:
10
10,
10,12-10
12-10

that is the valid strings are:
1. only integer
2. an integer, range of integers example 3

and no other characters must be allowed.


ereg("^[-0-9,]+$"...

would do it, but it would also allow "10,-" as a valid expression as 
well.. so if you want to check for valid ranges you'll want to beef that 
up a bit...


-philip

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] regular expression for integer range

2005-09-06 Thread babu
Hi all,
 
 
I want to write regular expression for checking the string format entered by 
user.
 
the allowed formats are
 
examples:
10
10,
10,12-10
12-10
 
that is the valid strings are:
1. only integer
2. an integer, range of integers example 3
 
and no other characters must be allowed.
 
thanks
babu.
 


-
How much free photo storage do you get? Store your holiday snaps for FREE with 
Yahoo! Photos. Get Yahoo! Photos