PREV UP NEXT Regex

3.6: List Operators ([ ... ] 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 as and ds 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.

]
ends the list if it's not the first list item. So, if you want to make the ] character a list item, you must put it first.
\
quotes the next character if the syntax bit RE_BACKSLASH_ESCAPE_IN_LISTS is set.
[:
represents the open-character-class operator (see Character Class Operators) if the syntax bit RE_CHAR_CLASSES is set and what follows is a valid character class expression.
:]
represents the close-character-class operator if the syntax bit RE_CHAR_CLASSES is set and what precedes it is an open-character-class operator followed by a valid character class name.
-
represents the range operator (see Range Operator) if it's not first or last in a list or the ending point of a range.

All other characters are ordinary. For example, [.*] matches . and *.

  • Character Class Operators [:class:]
  • Range Operator start-end

  • [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.