Re: regular expression for integer range
| From: | Robin Vickery | Date: | Wed, 07 Sep 2005 09:14:00 +0000 |
| Subject: | Re: regular expression for integer range | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-221946@lists.php.net to get a copy of this message | ||
On 9/6/05, babu <garavindbabu@yahoo.co.uk> 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