go to previous page   go to home page   go to next page


Does [\\\[] match \     ?   Yes

Does it match [     ?   Yes

Basic Regular Expressions: Ranges

Regular Expression
(Delimit with quotes)
(Delimit with quotes)

If you wanted to match any digit '0' through '9' you could use [0123456789], but that would be awkward. You can specify a range of characters by the following:

Rule 3.  Ranges of Characters

To show a range of characters, use square backets and separate the starting character from the ending character with a hyphen. For example, [0-9] matches any digit. Several ranges can be put inside square brackets. For example, [A-CX-Z] matches 'A' or 'B' or 'C' or 'X' or 'Y' or 'Z'.

Regular ExpressionMatches
[0-9] 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
[0-1][0-3] 00, 01, 02, 03, 10, 11, 12, 13
[a-d] a, b, c, d
Chapter [1-9] Chapter 1, Chapter 2, ...
[cC]hapter [1-9] chapter 1, Chapter 1, chapter 2, Chapter 2, ...
[A-Za-z][1-9] A9, a1, b8, W7, ...

The character range feature follows the collating sequence of the character set being used, usually ASCII. [a-z] includes all lower case alphabetic characters because those characters appear in sequence in ASCII.

Detail: If the dash character is the first one in the list, then it is treated as an ordinary character. For example [-AZ] matches '-' or 'A' or 'Z' . And tag[- ]line matches "tag-line" and "tag line" as in a previous example.

Detail: The characters connected by the dash must be in order. For example, [9-0] is not valid.

Bug Alert! [A-z] specifies all those characters between character capital 'A' and lower case 'z' in the ASCII sequence. This range includes several non-alphabetic characters that lie between 'Z' and 'a' in that sequence.


Write a RE that matches any alphabetic character, upper or lower case, and nothing else. (Hint: use two ranges.)