Lesson 4: Excluding specific characters

In some cases, we might know that there are specific characters that we don't want to match too, for example, we might only want to match phone numbers that are not from the area code 650.

To represent this, we use a similar expression that excludes specific characters using the square brackets and the ^ (hat). For example, the pattern [^abc] will match any single character except for the letters a, b, or c.

With the strings below, try writing a pattern that matches only the live animals (hog, dog, but not bog). Notice how most patterns of this type can also be written using the technique from the last lesson as they are really two sides of the same coin. By having both choices, you can decide which one is easier to write and understand when composing your own patterns.

Exercise 4: Excluding characters
Task Text  
match hog To be completed
match dog To be completed
skip bog To be completed
Solution

The simplest solution to match any line that ends in 'og' but is not 'bog' would be the expression [^b]og. Alternatively, you could use what we learned from the previous lesson and use [hd]og to match 'hog' and 'dog' but not 'bog'. Note that it is slightly more restrictive expression because it limits the strings it can match.

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