[...] (Character lists) |
Top Previous Next |
Specifies a list of characters that are valid in the string. Characters can be listed individually or a range of characters can be indicated by giving two characters and separating them by a '-'. For example, '[abc]' will match any of the characters 'a', 'b', or 'c'; this is the same as '[a-c]', which uses a range to express the same set of characters.
You can match the characters not within a range by complementing the set. This is indicated by using a '^' as the first character in the list (note: this is the same character as the beginning of string character). For example, '[^5]' will match any character except '5'.
Multiple ranges are allowed. E.g., the list '[A-Z0-9]' is a common way to express the idea of "all alphanumeric characters." To include one of the characters `\', `]', `-' or `^' in a character list use the '\' escape character.
For example:
The expression '[0-9]+' will find any number in a string (note the use of the '+' occurrence character).
The expression '[0-9]+/[0-9]+/[0-9]+' is a expression that could be used to search for dates within a string. Although it does not check the validity of the date it is unlikely that any other string would be match that pattern.
|