In a regular expression (RE), an expression within square brackets ( [ ] ) is a bracket expression. Within a bracket expression, the following characters lose their special meaning:
. [ \ *
A bracket expression itself contains one or more expressions that represent characters, collating symbols, equivalence or character classes, or range expressions:
| Bracket Expression | Description |
|---|---|
| [string] | Matches any of the characters specified. For example, [abc] matches any of a, b, or c. |
| [^string] | Does not match any of the characters in string. The caret immediately following the left bracket ( [ ) negates the characters that follow. For example, [^abc] matches any character or collating element except a, b, or c. |
| [collat_sym-collat_sym] | Matches any collating
elements that fall between the two specified collating
symbols, inclusive. The two symbols must be different,
and the second symbol must collate equal to or higher
than the first. For example, in the C locale, [r-t] would
match any of r, s, or t. To treat the hyphen (-) as itself, place it either first or last in the bracket expression, for example: [-rt] or [rt-]. Both of these expressions would match -, r, or t. |
| [ [.collat_symbl.] ] | Matches the collating
element represented by the specified single or
multicharacter collating symbol collat_symbl. For
example, assuming that <ch> is the collating symbol
for ch in the current locale, [ [.ch.] ] matches the
character sequence ch. (In contrast, [ch] matches c or
h.) If collat_symbl is not a collating element in the current locale, or if it has no characters associated with it, it is treated as an invalid expression. |
| [ [=collat_symbl=] ] | Matches all collating
elements that have a weight equivalent to the specified
single or multicharacter collating symbol collat_symbl.
For example, assuming a, à, and â belong to the same
equivalence class, [ [=a=] ] matches any of the three. If the collating symbol does not have any equivalents, it is treated as a collating symbol and matches its corresponding collating element (as for [..] ). |
| [ [:char_class:] ] | Matches any characters that belong to
the specified character class char_class. For example, [
[:alnum:] ] matches all alphanumeric characters
(characters for which isalnum would return nonzero). |
To use the right bracket ( ] ) in a bracket expression, you must specify it immediately following the left bracket ( [ ) or caret symbol (^). For example, [ ]x] matches the characters ] and x; [^]x] does not match ] or x; [x] ] is not valid.
You can combine characters, special characters, and bracket expressions to form REs that match multiple characters and subexpressions. When you concatenate the characters and expressions, the resulting RE matches any string that matches each component within the RE. For example, cd matches characters 3 and 4 of the string abcde; ab[ [:digit:] ] matches ab3 but not abc. For EREs, you can optionally enclose the concatenation in parentheses.