Re: OT - Regular Expression

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

 



# lists@xxxxxxxxxxx / 2007-02-09 14:13:27 +0200:
> 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.

This is the basic problem of regular expressin parsers, and you are
trying to write something that has the same behavior (if different
syntax).

This shouldn't do too much backtracking, try it out:

"*8*" => /^(?:\d*8\d*){4}$/

> 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?

The regular expression "abc" matches "a" AND "b" AND "c".  Sort of.
There's also lookahead and lookbehind assertions.

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE.             http://bash.org/?255991

-- 
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