PHP Regular Expression subpatterns

Description

Parentheses inside regular expressions define subpatterns that should be matched individually.

Subpatterns allow you to match very specific criteria.

For example, "(I|You) like (PHP|Java)" would match

  • "I like PHP",
  • "I like Java",
  • "You like PHP", and
  • "You like Java".

Syntax

() creates subpatterns.

Example 1

The following table shows a list of regular expressions using parentheses, and whether or not a match is made.

RegexpResult
print preg_match("/(PHP|Mac OS X)/","PHP")True
print preg_match("/(PHP|Mac OS X){2}/", "Mac OS X PHP")True
print preg_match("/(PHP|Mac OS X){2}/","Mac OS X PHP")False; there's a space in there, which is not part of the regexp
preg_match("/P(HP|ython)/","Python")True
preg_match("/Windows ([0-9][0-9]+|Me|XP)/", "Windows 2000")True; matches Windows 95, Windows 98, Windows 2000, 2003, Me, and XP
preg_match("/Windows (([0-9][0-9]+|Me|XP)|Codename (Vista|7))/","Windows Codename Vista")True;

Use Subpatterns to Group Patterns

We can use quantifiers (such as * and ?) to match the whole subpattern a certain number of times. For example:


<?PHP
echo preg_match( "/(row,? )+your boat/", "row, row, row your boat" );   
?>

The subpattern in this regular expression is "(row,?)". The letters 'r', 'o', and 'w', followed by either zero or one comma, followed by a space character.

This subpattern is matched at least one time because of the following + quantifier.

We can retrieve the individual subpattern matches in the matches array passed to preg_match().

The first element of the array contains the entire matched text as usual, and each subsequent element contains any matched subpatterns:


<?PHP/*  ww  w  .ja v a 2  s . c o m*/
   preg_match( "/(\d+\/\d+\/\d+) (\d+\:\d+.+)/", "7/18/2013 9:21AM", $matches ); 
   echo $matches[0] . "\n";  // Displays "7/18/2013 9:21AM" 
   echo $matches[1] . "\n";  // Displays "7/18/2013" 
   echo $matches[2] . "\n";  // Displays "9:21AM"    
?>




















Home »
  PHP Tutorial »
    Development »




Environment
Error
Hash
Include
Locale
Math
Network
Output
Reflection
PHP Regular Expressions