Lesson 3: Matching specific characters

The dot metacharacter from the last lesson is pretty powerful, but sometimes too powerful. If we are matching phone numbers for example, we don't want to validate the letters "(abc) def-ghij" as being a valid number!

There is a method for matching specific characters using regular expressions, by defining them inside square brackets. For example, the pattern [abc] will only match a single a, b, or c letter and nothing else.

Below are a couple lines, where we only want to match the first three strings, but not the last three strings. Notice how we can't avoid matching the last three strings if we use the dot, but have to specifically define what letters to match using the notation above.

Exercise 3: Matching characters
Task Text  
match can To be completed
match man To be completed
match fan To be completed
skip dan To be completed
skip ran To be completed
skip pan To be completed
Solution

You can use the expression [cmf]an to match only 'can', 'man' and 'fan' without matching any other line. As you will see in the next lesson, you can also use the inverse expression [^drp]an to match any three letter word ending with 'an' that does not start with 'd', 'r' or 'p'.

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