note 5439 modified in pcre.pattern.syntax by irc-html

From: Date: Mon, 21 Jan 2002 19:30:41 +0000
Subject: note 5439 modified in pcre.pattern.syntax by irc-html
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-25647@lists.php.net to get a copy of this message
<? // preg_match_all_test.php // // here is preg_math_all test from // http://www.php.net/manual/function.preg-match-all.php3 // -by mochaexpress@yahoo.com aka mocha // We use the default order of: // PREG_PATTERN_ORDER // Orders results so that $matches[0] is an array of full // pattern matches, $matches[1] is an array of strings matched // by the first parenthesized subpattern, and so on. $matches = preg_match_all("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x", "Call 555-1212 or 1-800-555-1212 or (612) 555-1313", $phones); /* / marks the start and end of the pattern match. characters after are pattern modifiers (see 'x' below). x pcre_extended. ignore white spaces in the pattern. see also http://www.php.net/manual/pcre.pattern.modifiers.php3 Extra spaces in the pattern are used for readability. \(? match zero or one open parenthesis. since '(' is a special character in regex, you need to escape it with a '\'. '?' matches 0 or 1 character, in this case a '('. (\d{3})? look for zero or one group of 3 digits. in other words, that one group of 3 digits may or may not exist. if it exists, memorize it. '\d' means a digit. '{3}' means 3 of what came before it; in this case, a digit. '{x,y}' signifies a range from 'x' to 'y'. you can leave out the 'y' (i.e. {x}) and it would mean 'x' number of 'characters' before it. '( )' in this case memorizes the 3 digits if it's found (back reference). '?' means 0 or 1 of the match before it. \)? match zero or one close parenthesis. since ')' is a special character in regex, you need to escape it with a '\'. '?' matches 0 or 1 character, in this case a ')'. (?(1) [\-\s] ) Here we have a Conditional Sub-Pattern. Two syntaxes for Conditional Sub-Pattern are: (?(condition)yes-pattern) (?(condition)yes-pattern|no-pattern) We use the first syntax. The condition is '(1)' which is our firt memorized (back reference) 3 digits matched by '(\d{3})?'. If there was a match of 3 digits, we look for the yes-pattern '[\-\s]'. '[]' is a character class that matches any single character within it. The '-' is a special regex character so we have to escape it with the back slash. '\s' means any white space. What this all means is if we found 3 digits, then look for a space or a dash to follow. If we didn't find the 3 digits, then we don't have to look for either one. \d{3}-\d{4} Now we are looking for 3 digits, a dash, then 4 digits. '\d' means a single digit. The number between '{}' is how many digits we are looking for. Output of the program in your browser should look like this: total of matches found = 3 total of phone numbers found = 3 phone 0 555-1212 phone 1 800-555-1212 phone 2 (612) 555-1313 SEE ALSO: http://www.php.net/manual/pcre.pattern.syntax.php3 */ $total = count($phones[0]); echo "total of matches found = ".$matches."<BR>\n"; echo "total of phone numbers found = ".$total."<BR>\n"; for ($i = 0; $i < $total; $i++) { echo "phone ".$i." ".$phones[0][$i]."<BR>\n"; } ?> --was-- <PRE> &lt;? // preg_match_all_test.php // // here is preg_math_all test from // http://www.php.net/manual/function.preg-match-all.php3 // -by mochaexpress@yahoo.com aka mocha // We use the default order of: // PREG_PATTERN_ORDER // Orders results so that $matches[0] is an array of full // pattern matches, $matches[1] is an array of strings matched // by the first parenthesized subpattern, and so on. $matches = preg_match_all("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x", "Call 555-1212 or 1-800-555-1212 or (612) 555-1313", $phones); /* / marks the start and end of the pattern match. characters after are pattern modifiers (see 'x' below). x pcre_extended. ignore white spaces in the pattern. see also http://www.php.net/manual/pcre.pattern.modifiers.php3 Extra spaces in the pattern are used for readability. \(? match zero or one open parenthesis. since '(' is a special character in regex, you need to escape it with a '\'. '?' matches 0 or 1 character, in this case a '('. (\d{3})? look for zero or one group of 3 digits. in other words, that one group of 3 digits may or may not exist. if it exists, memorize it. '\d' means a digit. '{3}' means 3 of what came before it; in this case, a digit. '{x,y}' signifies a range from 'x' to 'y'. you can leave out the 'y' (i.e. {x}) and it would mean 'x' number of 'characters' before it. '( )' in this case memorizes the 3 digits if it's found (back reference). '?' means 0 or 1 of the match before it. \)? match zero or one close parenthesis. since ')' is a special character in regex, you need to escape it with a '\'. '?' matches 0 or 1 character, in this case a ')'. (?(1) [\-\s] ) Here we have a Conditional Sub-Pattern. Two syntaxes for Conditional Sub-Pattern are: (?(condition)yes-pattern) (?(condition)yes-pattern|no-pattern) We use the first syntax. The condition is '(1)' which is our firt memorized (back reference) 3 digits matched by '(\d{3})?'. If there was a match of 3 digits, we look for the yes-pattern '[\-\s]'. '[]' is a character class that matches any single character within it. The '-' is a special regex character so we have to escape it with the back slash. '\s' means any white space. What this all means is if we found 3 digits, then look for a space or a dash to follow. If we didn't find the 3 digits, then we don't have to look for either one. \d{3}-\d{4} Now we are looking for 3 digits, a dash, then 4 digits. '\d' means a single digit. The number between '{}' is how many digits we are looking for. Output of the program in your browser should look like this: total of matches found = 3 total of phone numbers found = 3 phone 0 555-1212 phone 1 800-555-1212 phone 2 (612) 555-1313 SEE ALSO: http://www.php.net/manual/pcre.pattern.syntax.php3 */ $total = count($phones[0]); echo "total of matches found = ".$matches."<BR>\n"; echo "total of phone numbers found = ".$total."<BR>\n"; for ($i = 0; $i < $total; $i++) { echo "phone ".$i." ".$phones[0][$i]."<BR>\n"; } ?> </PRE> http://www.php.net/manual/en/pcre.pattern.syntax.php

« previous php.notes (#25647) next »