Many times it's useful to match specified characters from a list. For example, you may want to search a string for chapter headings that are expressed as Chapter 1, Chapter 2, etc.
You can create a list of matching characters by placing one or more individual characters within square brackets ([ and ]). When characters are enclosed in brackets, the list is called a bracket expression.
Within brackets, as anywhere else, ordinary characters represent themselves, that is, they match an occurrence of themselves in the input text. Most special characters lose their meaning when they occur inside a bracket expression. There are some exceptions:
The ] character ends a list if it is not the first item. To match the ] character in a list, place it first, immediately following the opening [.
The \ character continues to be the escape character. To match the \ character itself, use \\.
Characters enclosed in a bracket expression match only a single character for the position in the Regular Expression where the bracket expression appears. The following Regular Expression matches 'Chapter 1', 'Chapter 2', 'Chapter 3', 'Chapter 4', and 'Chapter 5':
/Chapter [12345]/
If you want to express the matching characters using a range instead of the characters themselves, you can separate the beginning and ending characters in the range using the hyphen (-) character. The Unicode character value of the individual characters determines their relative order within a range. The following Regular Expression is equivalent to the bracketed list shown above.
/Chapter [1-5]/
When a range is specified in this manner, both the starting and ending values are included in the range. It is important to note that the starting value must precede the ending value in Unicode sort order.
If you want to include the hyphen character (-) in your bracket expression, you must do one of the following:
Escape it with a backslash:
[\-]
Put the hyphen character at the beginning or the end of the bracketed list. The following expressions match all lowercase letters and the hyphen:
[-a-z][a-z-]
Create a range where the beginning character value is lower than the hyphen character and the ending character value is equal to or greater than the hyphen. Both of the following Regular Expressions satisfy this requirement:
[!--][!-~]
i.e. characters from ! to – or from ! to ~.
If you want to find all the characters not in the list or range, you can place the caret (^) character at the beginning of the list. If the caret character appears in any other position within the list, it matches itself, that is, it has no special meaning. The following Regular Expression matches chapter headings with numbers different from 1,2,3,4 and 5 (and any other characters different from 1,2,3,4 and 5):
/Chapter [^12345]/
The same expressions above can be represented using the hyphen character (-).
/Chapter [^1-5]/
A typical use of a bracket expression is to specify matches of any upper- or lowercase alphabetic characters or any digits. The following regex specifies such a match:
/[A-Za-z0-9]/
Character | Description |
| A character set. Matches any one of the enclosed characters. |
| A negative character set. Matches any character not enclosed. |
| A range of characters. Matches any character in the specified range. |
| A negative range characters. Matches any character not in the specified range. |
a, x, y and z represent characters.