Special characters are commonly used in regular expressions. The period is a special character that matches one instance of the specified character. It must be escaped with a backslash to be interpreted as a period. In the following expression, the period is replaced with an exclamation point.
Regex( "Bicycling makes traveling to work fun.", "\.", "!", GLOBALREPLACE );
"Bicycling makes traveling to work fun!"
Table 6.8 describes the special characters and provides examples.
\ |
• Precedes a literal character. <\/a> interprets the forward slash literally in the end HTML anchor tag. • Precedes an escape sequence. \n matches a newline character. |
^ |
Matches the beginning of a string, not including the newline character. ^apple matches “apple” at the beginning of a string. |
$ |
Matches the end of a string, not including the newline character. apple$ matches “apple” at the end of a string. |
. |
Matches any single character including a newline character. .apple matches any single character and then “apple”. |
| |
Represents a logical OR to separate alternative values. (apple|orange|banana) matches “apple”, “orange”, or “banana”. |
? |
Matches zero or one instance. apple (pie)? matches one or more instances of “pie”. |
* |
Matches zero or more instances. |
+ |
Matches one or more instances. |
( ) |
Encloses a sub-expression. (apple|orange|banana) matches “apple”, “orange”, or “banana”. ^(\w+) matches the beginning of a line and then one or more word characters. |
[ ] |
Encloses an expression that matches set of characters. [\s] matches a whitespace character or a digit. [a-z0-9] matches “a” through “z” and numbers “0” through “9”. |
{ } |
Encloses an expression that represents repetition. apple{3} repeats three times. apple{3,} repeats at least three times as many times as possible. apple{3, 10} repeats three times but no more than 10 times. Append a question mark to indicate repeating as few times as possible. For example, apple{3,}? repeats at least three times as few times as possible. |