Character classes -- enclosed in square brackets -- [...]
A character class is a list of characters, any of which can be matched. The list can also be excluded from matches. Ranges of ASCII characters can also be specified.
Expression | Matches |
Does not match
[gs]et
|
get, set
(only)
| (anything else)
| |
Choosing from a range of alphanumeric characters
A minus sign (-) within square brackets indicates a range of consecutive ASCII characters. For example, [0-9] is the same as [0123456789].
Expression | Matches |
Does not match
Do[A-Za-z]*Command
|
DoCommand
| DoMenuCommand DomouseCommand...
Do-Command
| Do88Command abc... |
Excluding a character list
If the first character in the square brackets is a caret (^), any character except those in the square brackets will match.
Expression | Matches |
Does not match
Do[^\s(Cc]
|
DoMenuCommand
| DomouseCommand Domino
Do
(followed by space)
| Do( DoCommand abc ... |
Metacharacters inside square brackets
To include the minus sign itself in a range, it must be the first character (after an initial ^, if any -- see Excluding a character list), e.g., Do[-A-Za-z]*Command would also match
Expression | Matches |
Does not match
foo([^)]+)
|
foo(a)
| foo( ) my foo(int a, int b, c)...
foo (a)
| foo() ...
<foo\s*(\s*[^)\s][.\n]*])
| any
foo
with at | least 1 parameter (anything else)
| |