An ordinary character in a regular expression matches itself. The ordinary characters are characters other than those described as special characters.
( ) [ ] ^ $ . * + ? | \
The special characters affect the matching behavior of regular expressions, as described in the table. Constructs that match arbitrary-length character sequences, such as * + ?, always match the longest leftmost string that permits a match.
Character | Meaning | Example |
---|---|---|
^ | Matches the beginning of the string | "^abc" matches strings starting with abc |
$ | Matches the end of the string | "abc$" matches strings ending with abc |
. | Matches any single character | "a.c$" matches strings containing abc, axc, and so on |
* | Matches zero or more of the immediately preceding expression | "ab*c$" matches strings containing ac, abc, abbc, and so on |
+ | Matches one or more of the immediately preceding expression | "a+c$" matches strings containing abc, abbc, aggc, but not ac |
? | Matches zero or one of the immediately preceding expression | "ab?c" matches strings containing ac or abc |
| | Matches either the preceding or following expression. | "a|b|c" matches strings containing a, b, or c |
[ ] | Matches any single character listed between brackets | "[ab]c" matches strings containing ac or bc |
[^ ] | Matches any single character not listed between brackets. | "a[^b]c" matches strings containing axc for any replacement of x except for b |
\ | Escapes the character which immediately follows. To embed a backslash character in a string, the string literal must contain two consecutive backslashes. | "a\.c$" matches strings containing a.c, and "a\\c$" matches strings containing a\c |
( ) | Delimits subexpressions | "a(b|c)*d*" matches strings containing a followed by any number of bs or cs followed by d, such as "ad" or "acbbccd" |