Re: Re: Can someone help me build a regular expression?
| From: | Jochem Maas | Date: | Tue, 03 May 2005 00:07:03 +0000 |
| Subject: | Re: Re: Can someone help me build a regular expression? | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-214356@lists.php.net to get a copy of this message | ||
Rory Browne wrote:
mites byte.You mite try this. I know that this work with perl.
er?=~ m/^[0-9][A-Z][a-z]{2-3} \.[0-9]+$/I'm not sure what the initial m does(I'm not a perl person), but the rest of the regex matches as follows. A string whose first character is a digit between 0 and 9. This is followed by an Upper case letter, and then two or three lower case letters. All that is followed by a period(or dot), after which may be
a single-digit number, but nothing else. It matches the following: 1Abc.2 1Abcd.2 1Abc. 1Abcd.try this and see if that is correct: php -r ' $a = array( "gnaglreg1Abc.2ewfergwrgbt","1Abc.2","1Abcd.2","1Abc.","1Abcd.","1Abcd .2","1Abcd .2", "1Abcd.22","1Abcd .22","1Abcd .22","1Abcd.232"); foreach ($a as $s) { echo "\ntrying:\t\t\"$s\":\n--\n"; echo preg_replace("/^[0-9][A-Z][a-z]{2-3} \.[0-9]+$/", "..!!GOTCHA1!!..", $s),"\n"; echo preg_replace("/^[0-9][A-Z][a-z]{2,3} \.[0-9]+$/", "..!!GOTCHA2!!..", $s),"\n"; echo preg_replace("/^[0-9][A-Z][a-z]{2,3} \.[0-9]?$/", "..!!GOTCHA3!!..", $s),"\n"; echo preg_replace("/^[0-9][A-Z][a-z]{2,3}\.[0-9]+$/", "..!!GOTCHA4!!..", $s),"\n"; echo preg_replace("/^[0-9][A-Z][a-z]{2,3}\.[0-9]?$/", "..!!GOTCHA5!!..", $s),"\n"; echo preg_replace("/^[0-9][A-Z][a-z]{2,3}[ ]?\.[0-9]+$/", "..!!GOTCHA6!!..", $s),"\n"; echo preg_replace("/^[0-9][A-Z][a-z]{2,3}[ ]{1,}\.[0-9]+$/", "..!!GOTCHA7!!..", $s),"\n"; echo preg_replace("/^[0-9][A-Z][a-z]{2,3}[ ]?\.[0-9]?$/", "..!!GOTCHA8!!..", $s),"\n"; echo preg_replace("/^[0-9][A-Z][a-z]{2,3}[ ]{1,}\.[0-9]?$/", "..!!GOTCHA9!!..", $s),"\n"; echo "---\n"; } '
run the above code, examine the output (if you are going to do it in a browser echo out some <pre> tags). hopefully the 9 variations will give you a little bit of insight. read every page here: http://nl2.php.net/manual/en/ref.pcre.php - its a good primer on regexp. don't worry if you don't understand half of it. often things become clear as you continue to read/experiment :-)I'm still very new to this. But I'm trying to help.
Thomas freeswimfreak@yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php