Capturing with nested parentheses : preg_match « String « PHP






Capturing with nested parentheses

 
<?
if (preg_match('/^(\d{5})(-(\d{4}))?$/',$_POST['zip'],$matches)) {
    print "The beginning of the ZIP Code is: $matches[1]\n";
    if (isset($matches[2])) {
        print "The ZIP+4 is: $matches[3]";
    }
}
?>
  
  








Related examples in the same category

1.Capturing with preg_match()
2.Check quotes in a file
3.Combine patterns with the pipe (|) character to create branches
4.Checking the syntax of an e-mail address
5.preg_match
6.preg_match( ) calls and what they match
7.preg_match() function returns true if pattern exists, and false otherwise.
8.Matching Patterns with preg_match()
9.Matching with preg_match()
10.PREG_OFFSET_CAPTURE and preg_match()
11.Using preg_match to return an array of matches that start with ple
12.Detecting ZIP Codes in Strings
13.Looking for any lowercase alphabetical character or the numbers 3, 4, and 7
14.Efficiently finding lines that match a pattern
15.Find the first "p" string and match as many characters as possible until the last possible "t" is reached.
16.Finding All Matching Lines in a File
17.Finding Lines with an Odd Number of Quotes
18.Finding Repeated Words
19.Finding Words Not Followed by Other Words
20.Finding the nth Occurrence of a Match
21.Get the start and end of the string when m is enabled, you should use \A and \z, like this:
22.Negate the characters in the character class
23.Parsing a date with a regular expression
24.Words and Whitespace Regexps
25.i renders the regular expression non case sensitive
26.s modifier: use . to match characters across multiple lines.
27.Regular expressions using +, *, and ?
28.Regular expressions using braces
29.Put the word boundary character to the test
30.Validating numbers with regular expressions
31.Use regular expression to check each line of the code
32.Testing the Complexity of Passwords