Re: Regexp help (simple)
| From: | Dagfinn Reiersøl | Date: | Tue, 20 Jan 2004 09:41:03 +0000 |
| Subject: | Re: Regexp help (simple) | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-175112@lists.php.net to get a copy of this message | ||
php@humbapa.ch wrote:
$string = 'ab12345-1'; if (preg_match('/^([a-zåäö]{2,3})([0-9]{4,5}(\-[0-9]{1,2}){0,1})$/i', $string, $m='')) { echo $m[1]; // -> ab echo $m[2]; // -> 12345-1 } g. martin luethi You can replace {0,1} with a question mark and [0-9] with \d (digit). Also, and Ithink this is not in the PHP documentation, you can use POSIX character classes inside the brackets. If you want to match alphabetical characters including the Swedish and various other international ones like æ or ü, you can use [:alpha:]. You may not need it in this example, but it's excellent for internationalized regex matching. if (preg_match('/^([[:alpha:]]{2,3})(\d{4,5}(\-\d{1,2})?)$/i',
Tue, 20 Jan 2004 09:59:37 +0100 Victor Spång Arthursson <scooterbabe@home.se>:Hi! Anyone who could help me with this regexp problem? I want to verify that a string is made up of 2-3 letters, (a-z + åäö, A-Z + ÅÖÄ), directly followed by 4 or 5 digits, which could, but may not, be followed by a minus and one or two digits. Examples of valid strings: abc12345 ABC12345 abc1234 ABC12345-1 ABC12345-01 I would also like to split them into an array consisting of 2 elements; [0] = the first 2 or 3 letters [1] = the rest Example: string = ab12345 [0] = ab [1] = 12345 string = åäö1234-66 [0] = åäö [1] = 1234-66 Lots of thanks in advance, sincerely Victor -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php