<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Meta-characters</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="regexp.reference.delimiters.html">Delimiters</a></div> <div class="next" style="text-align: right; float: right;"><a href="regexp.reference.escape.html">Escape sequences</a></div> <div class="up"><a href="reference.pcre.pattern.syntax.html">PCRE regex syntax</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="regexp.reference.meta" class="section"> <h2 class="title">Meta-characters</h2> <p class="para"> The power of regular expressions comes from the ability to include alternatives and repetitions in the pattern. These are encoded in the pattern by the use of <em class="emphasis">meta-characters</em>, which do not stand for themselves but instead are interpreted in some special way. </p> <p class="para"> There are two different sets of meta-characters: those that are recognized anywhere in the pattern except within square brackets, and those that are recognized in square brackets. Outside square brackets, the meta-characters are as follows: <table class="doctable table"> <caption><strong>Meta-characters outside square brackets</strong></caption> <thead> <tr> <th>Meta-character</th><th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>\</td><td>general escape character with several uses</td> </tr> <tr> <td>^</td><td>assert start of subject (or line, in multiline mode)</td> </tr> <tr> <td>$</td><td>assert end of subject or before a terminating newline (or end of line, in multiline mode)</td> </tr> <tr> <td>.</td><td>match any character except newline (by default)</td> </tr> <tr> <td>[</td><td>start character class definition</td> </tr> <tr> <td>]</td><td>end character class definition</td> </tr> <tr> <td>|</td><td>start of alternative branch</td> </tr> <tr> <td>(</td><td>start subpattern</td> </tr> <tr> <td>)</td><td>end subpattern</td> </tr> <tr> <td>?</td><td>extends the meaning of (, also 0 or 1 quantifier, also makes greedy quantifiers lazy (see <a href="regexp.reference.repetition.html" class="link">repetition</a>)</td> </tr> <tr> <td>*</td><td>0 or more quantifier</td> </tr> <tr> <td>+</td><td>1 or more quantifier</td> </tr> <tr> <td>{</td><td>start min/max quantifier</td> </tr> <tr> <td>}</td><td>end min/max quantifier</td> </tr> </tbody> </table> Part of a pattern that is in square brackets is called a <a href="regexp.reference.character-classes.html" class="link">character class</a>. In a character class the only meta-characters are: <table class="doctable table"> <caption><strong>Meta-characters inside square brackets (<em class="emphasis">character classes</em>)</strong></caption> <thead> <tr> <th>Meta-character</th><th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>\</td><td>general escape character</td> </tr> <tr> <td>^</td><td>negate the class, but only if the first character</td> </tr> <tr> <td>-</td><td>indicates character range</td> </tr> </tbody> </table> The following sections describe the use of each of the meta-characters. </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="regexp.reference.delimiters.html">Delimiters</a></div> <div class="next" style="text-align: right; float: right;"><a href="regexp.reference.escape.html">Escape sequences</a></div> <div class="up"><a href="reference.pcre.pattern.syntax.html">PCRE regex syntax</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>