Re: OT - Regular Expression

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 2007-02-09 at 14:13 +0200, Peter Lauri wrote:
> Best group member,
> 
>  
> 
> I want to match a four digit number. I allow user to enter with * syntax. So
> 8* would match anything that starts with 8 and is 4 digit long so:
> 
>  
> 
> /^8[0-9]{3}$/
> 
>  
> 
> That was easy. Ok then my other case was: *8, so anything that ends with 8
> 
>  
> 
> /^[0-9]{3}8$/
> 
>  
> 
> Ok, now the tricky one comes: *8*, so match it incase 8 is anywhere in the
> number. Can be beginning, end or in the middle. The problem that I face I
> cannot find out a good way of doing this correctly. So I ended up with an
> expression like this:
> 
>  
> 
> /^(8[0-9]{3}|[0-9]8[0-9]{2}|[0-9]{2}8[0-9]|[0-9]{3}8)$/
> 
>  
> 
> This takes care of it and everything, BUT it is so ugly. What I actually
> need to construct is: A regular expression that checks if 8 is a part of the
> number, and then that it is four digit long.
> 
>  
> 
> The pipe "|" is an OR operator, but are there not any "AND" operator in
> Regular Expressions? I have been trying to figure this out for a while now.
> Of course I am using the above syntax right now, but would like to strip it
> down. Maybe not for the performance, but for the beauty of it :-)

Why not use two checks? One that checks for 4 digits, the other checks
for existence of 8 anywhere in the string?

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux