www.delorie.com/gnu/docs/regex/regex_17.html   search  
Buy the book!


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

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 `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 `[^'(2)) 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 section 7.1.1 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 section 3.6.1 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 section 3.6.2 The 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 `*'.

3.6.1 Character Class Operators ([: ... :])  [:class:]
3.6.2 The Range Operator (-)  start-end

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003