Lesson 5: Character ranges

We just learned how to create a pattern that matches or excludes specific characters -- but what if we want to match a character that can be in a sequential range characters? Do we have no choice but to list them all out?

Luckily, when using the square bracket notation, there is a shorthand for matching a character in list of sequential characters by using the dash to indicate a character range. For example, the pattern [0-6] will only match any single digit character from zero to six, and nothing else. And likewise, [^n-p] will only match any single character except for letters n to p.

Multiple character ranges can also be used in the same set of brackets, along with individual characters. An example of this is the alphanumeric \w metacharacter which is equivalent to the character range [A-Za-z0-9_] and often used to match characters in English text.

In the exercise below, notice how all the match and skip lines have a pattern, and use the bracket notation to match or skip each character from each line. Be aware that patterns are case sensitive and a-z differs from A-Z in terms of the characters it matches (lower vs upper case).

Exercise 5: Matching character ranges
Task Text  
match Ana To be completed
match Bob To be completed
match Cpc To be completed
skip aax To be completed
skip bby To be completed
skip ccz To be completed
Solution

All the characters are sequential, so you can use the different ranges in the expression [A-C][n-p][a-c] to match only the first three lines.

Solve the above task to continue on to the next problem, or read the Solution.