[
... ]
and [^
... ]
)Lists, also called bracket expressions, are a set of one or more items. An item is a character, a character class expression, or a range expression. The syntax bits affect which kinds of items you can put in a list. We explain the last two items in subsections below. Empty lists are invalid.
A matching list matches a single character represented by one of
the list items. You form a matching list by enclosing one or more items
within an open-matching-list operator (represented by [
)
and a close-list operator (represented by ]
).
For example, [ab]
matches either a
or b
.
[ad]*
matches the empty string and any string composed of just
a
s and d
s in any order. Regex considers invalid a regular
expression with a [
but no matching
]
.
Nonmatching lists are similar to matching lists except that they
match a single character not represented by one of the list
items. You use an open-nonmatching-list operator (represented by
[^
[1]) instead of an open-matching-list operator to start a
nonmatching list.
For example, [^ab]
matches any character except a
or
b
.
If the posix_newline
field in the pattern buffer (see GNU Pattern Buffers is set, then nonmatching lists do not match a newline.
Most characters lose any special meaning inside a list. The special characters inside a list follow.
]
]
character a list item, you must put it first.
\
RE_BACKSLASH_ESCAPE_IN_LISTS
is
set.
[:
RE_CHAR_CLASSES
is set and what
follows is a valid character class expression.
:]
RE_CHAR_CLASSES
is set and what precedes it is an
open-character-class operator followed by a valid character class name.
-
All other characters are ordinary. For example, [.*]
matches
.
and *
.
[1] Regex therefore doesn't consider the ^
to be the first character in the list. If you put a ^
character first in (what you think is) a matching list, you'll turn it into a nonmatching list.