| 1 | <html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 2 | <title>Regular expressions 1. Special characters</title> |
| 3 | <style> |
| 4 | td { padding-left: 5pt; padding-right: 5pt; } |
| 5 | th { padding-left: 5pt; padding-right: 5pt; } |
| 6 | code { white-space: pre-wrap; } |
| 7 | dd { white-space: pre-wrap; } |
| 8 | </style> |
| 9 | </head><body bgcolor="#FFFFFF"> |
| 10 | |
| 11 | <table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor="#CCCC00"><table border=4 cellpadding=9><tr><td align=middle bgcolor="#000000"><font face="Palatino,Times" size=6 color="#999900"><b> |
| 12 | Regular expressions 1. Special characters |
| 13 | </b></font></table></table> |
| 14 | <p>The following characters are the <i>meta</i> characters that give special meaning to the regular expression search syntax:</p> |
| 15 | <dl> |
| 16 | <dt><b></b>\<b></b> the backslash <i>escape</i> character. |
| 17 | <dd>The backslash gives special meaning to the character following it. For example, the combination "\n" stands for the <i>newline</i>, one of the <a href="Regular_expressions_5__Special_control_characters.html">control characters</a>. The combination "\w" stands for a "word" character, one of the <a href="Regular_expressions_6__Convenience_escape_sequences.html">convenience escape sequences</a> while "\1" is one of the <a href="Regular_expressions_8__Substitution_special_characters.html">substitution special characters</a>.</dd> |
| 18 | <dd> Example: The regex "aa\n" tries to match two consecutive "a"s at the end of a line, inclusive the newline character itself.</dd> |
| 19 | <dd> Example: "a\+" matches "a+" and not a series of one or "a"s.</dd> |
| 20 | <dt><b>^</b> the caret is the <a href="Regular_expressions_3__Anchors.html">anchor</a> for the start of the string, or the negation symbol. |
| 21 | <dd> Example: "^a" matches "a" at the start of the string.</dd> |
| 22 | <dd> Example: "[^0-9]" matches any non digit.</dd> |
| 23 | <dt><b>$</b> the dollar sign is the <a href="Regular_expressions_3__Anchors.html">anchor</a> for the end of the string. |
| 24 | <dd> Example: "b$" matches a "b" at the end of a line.</dd> |
| 25 | <dd> Example: "^$" matches the empty string.</dd> |
| 26 | <dt><b>{ }</b> the opening and closing curly brackets are used as range <a href="Regular_expressions_2__Quantifiers.html">quantifiers</a>. |
| 27 | <dd> Example: "a{2,3}" matches "aa" or "aaa".</dd> |
| 28 | <dt><b>[ ]</b> the opening and closing square brackets define a character class to match a <i>single</i> character. |
| 29 | <dd>The "^" as the first character following the "[" negates, and the match is for the characters <i>not</i> listed. The "-" denotes a range of characters. Inside a "[ ]" character class construction, most special characters are interpreted as ordinary characters.</dd> |
| 30 | <dd> Example: "[d-f]" is the same as "[def]" and matches "d", "e" or "f".</dd> |
| 31 | <dd> Example: "[a-z]" matches any lower-case characters in the alphabet.</dd> |
| 32 | <dd> Example: "[^0-9]" matches any character that is not an ASCII digit.</dd> |
| 33 | <dd> Example: A search for "[][()?<>$^.*?^]" in the string "[]()?<>$^.*?^" followed by a replace string "r" has the result "rrrrrrrrrrrrr". Here the search string is <i>one</i> character class and all the meta characters are interpreted as ordinary characters without the need to escape them.</dd> |
| 34 | <dt><b>( )</b> the opening and closing parenthes3s are used for grouping characters (or other regexes). |
| 35 | <dd>The groups can be referenced in both the search and the <a href="Regular_expressions_8__Substitution_special_characters.html">substitution</a> phase. There also exist some <a href="Regular_expressions_4__Special_constructs_with_parenthe.html">special constructs with parentheses</a>.</dd> |
| 36 | <dd> Example: "(ab)\1" matches "abab".</dd> |
| 37 | <dt><b>.</b> the dot matches any character except the newline symbol. |
| 38 | <dd> Example: ".a" matches two consecutive characters where the last one is "a".</dd> |
| 39 | <dd> Example: ".*\.txt$" matches all strings that end in ".txt".</dd> |
| 40 | <dt><b>*</b> the asterisk is the match-zero-or-more <a href="Regular_expressions_2__Quantifiers.html">quantifier</a>. |
| 41 | <dd> Example: "^.*$" matches an entire line. </dd> |
| 42 | <dt><b>+</b> the plus sign is the match-one-or-more quantifier. |
| 43 | <dt><b>?</b> the question mark is the match-zero-or-one quantifier. The question mark is also used in <a href="Regular_expressions_4__Special_constructs_with_parenthe.html">special constructs with parentheses</a> and in <a href="Regular_expressions_2__Quantifiers.html">changing match behaviour</a>. |
| 44 | <dt><b>|</b> the vertical pipe separates a series of alternatives. |
| 45 | <dd> Example: "(a|b|c)a" matches "aa" or "ba" or "ca".</dd> |
| 46 | <dt><b>< ></b> the smaller and greater signs are <a href="Regular_expressions_3__Anchors.html">anchors</a> that specify a left or right word boundary. |
| 47 | <dt><b>-</b> the minus sign indicates a range in a character class (when it is not at the first position after the "[" opening bracket or the last position before the "]" closing bracket. |
| 48 | <dd> Example: "[A-Z]" matches any uppercase character.</dd> |
| 49 | <dd> Example: "[A-Z-]" or "[-A-Z]" match any uppercase character or "-".</dd> |
| 50 | <dt><b>&</b> the ampersand is the "substitute complete match" symbol. |
| 51 | </dl> |
| 52 | <h3>Links to this page</h3> |
| 53 | <ul> |
| 54 | <li><a href="Regular_expressions.html">Regular expressions</a> |
| 55 | </ul> |
| 56 | <hr> |
| 57 | <address> |
| 58 | <p>© djmw 20010718</p> |
| 59 | </address> |
| 60 | </body> |
| 61 | </html> |