OT - Regular Expression
| From: | Peter Lauri | Date: | Fri, 09 Feb 2007 12:13:27 +0000 |
| Subject: | OT - Regular Expression | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-248636@lists.php.net to get a copy of this message | ||
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 :-)
If you have any comments and suggestions about this I would be happy.
Best regards,
Peter Lauri
<http://www.dwsasia.com/> www.dwsasia.com - company web
site
<http://www.lauri.se/> www.lauri.se - personal web site
<http://www.carbonfree.org.uk/>
www.carbonfree.org.uk - become Carbon Free