PHP Regular Expression Multiple Characters

Description

A quantifier is placed after the character or character class, and indicates how many times that character or class should repeat in the target string. The quantifiers are:

The metacharacters +, *, ?, and { } affect the number of times a pattern should be matched

Syntax

  • + means "Match one or more of the previous expression,"
  • * means "Match zero or more of the previous expression,"
  • ? means "Match zero or one of the previous expression."
  • {} can be used to define specific repeat counts in three different ways.
  • {n} means must occur exactly n times
  • {n,} means must occur at least n times
  • {n,m} means must occur at least n times but no more than m times

Example

The following table shows a list of regular expressions using +, *, and ?, and {}.

RegexpResult
preg_match("/[A-Z]+/", "123")False
preg_match("/[A-Z][A-Z0-9]+/i", "A123")True
preg_match("/[0-9]?[A-Z]+/", "10Green")True; matches "0G"
preg_match("/[0-9]?[A-Z0-9]*/i","10Green") True
preg_match("/[A-Z]?[A-Z]?[A-Z]*/", "")True; zero or one match, then zero or one match, then zero or more means that an empty string matches
preg_match("/[A-Za-z ]*/", "aaa");matches "", "a", "aaaa", etc
preg_match("/-?[0-9]+/", "1111");matches 1, 100, and also -1, etc.
preg_match("/[A-Z]{3}/", "FuZ")False; the regexp will match precisely three uppercase letters
preg_match("/[A-Z]{3}/i", "FuZ")True; same as above, but case-insensitive this time
preg_match("/[0-9]{3}-[0-9]{4}/", "555-1234")True; precisely three numbers, a dash, then precisely four.
preg_match("/[a-z]+[0-9]?[a-z]{1}/","aaa1")True; must end with one lowercase letter
preg_match("/[A-Z]{1,}99/", "99")False; must start with at least one uppercase letter
preg_match("/[A-Z]{1,5}99/","R9")True;
preg_match("/[A-Z]{1,5}[0-9]{2}/i", "a42")True

The "-?" means "match exactly 0 or 1 minus symbols".

Greedy and Non - Greedy Matching

Quantifiers for matching multiple characters are greedy by default. They will try to match the largest number of characters possible. Consider the following code:


<?PHP
preg_match( "/P.*r/", "Peter Piper", $matches ); 
echo $matches[0]; 
?>

The regular expression means, "Match the letter 'P' followed by zero or more characters of any type, followed by the letter 'r'. ".

Because quantifiers are greedy, the regular expression matches as many characters as it can between the first "P" and the last "r".

We can change a quantifier to be non-greedy. This causes it to match the smallest number of characters possible.

To make a quantifier non-greedy, place a question mark (?) after the quantifier. For example, to match the smallest possible number of digits use:

/\d+?/

Rewriting the Peter Piper example using a non-greedy quantifier gives the following result:


<?PHP
preg_match( "/P.*?r/", "Peter Piper", $matches ); 
echo $matches[0]; 
?>

Here, the expression matches the first letter "P" followed by the smallest number of characters possible ("ete"), followed by the first letter "r".





















Home »
  PHP Tutorial »
    Development »




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