Regular Expression Characters
Purpose | Character | Description | Example |
---|---|---|---|
Wildcard matching | . (period) |
Match 1 character | a.t matches act and apt but not abort |
Matching zero or more occurrences | * (asterisk) |
Match 0 or more occurrences of preceding character or expression | 0*1 matches 1, 01, 001, etc. a.* matches act, apt, and abort |
+ (plus sign) |
Match 1 or more occurrences of preceding character or expression | 0+1 matches 01, 001, 0001, . . . |
|
Matching either/or | ? (question mark) |
Match 0 or 1 occurrences of preceding character or expression | 0?1 matches 1 and 01 but not 001 |
| (pipe) |
Match either the preceding or following character or expression | a|b matches every occurrence of a or b abor|ut matches every occurrence of abort or about {if}|{else} matches every occurrence of if or else |
|
Matching the beginning or ending of a line | ^ (caret) |
Match the beginning of a line | ^int matches any line that begins with int |
$ (dollar sign) |
Match the end of a line | end$ matches any line that ends with end | |
Grouping expressions | {} (curly braces) |
Group characters or expressions for searches | {if}|{else} matches every occurrence of if or else |
Matching a set | [] (brackets) |
Match any one character or range listed within the brackets | [a-z] matches every occurrence of lowercase letters [abc] matches every occurrence of a, b, or c |
^ (caret) |
If appears immediately after the left bracket, negate the contents of the set | [^a-z] matches everything except lowercase letters [a-z^A-Z] matches all letters and the '^' character |
|
Special characters | \t (backslash t) |
Match any tab character | \t3 matches every occurrence of a tab character followed by a 3 |
\x (backslash x) |
Match any character specified in hex | \x2a matches every occurrence of the '*' character | |
\w (backslash w) |
Match any word character | a\wt matches act and apt but not a-t | |
\W (backslash W) |
Match any non-word character | a\Wt matches a-t and a.t but not act | |
\s (backslash s) |
Match any white-space character | \s3 matches every occurrence of a white-space character followed by a 3 | |
\S (backslash S) |
Match any non-white space character | \S3 matches every occurrence of a non white-space character followed by a 3 | |
\ (backslash) |
Include the subsequent regular expression character in the search | \-\? matches every occurrence of '-' followed by '?' |