Lesson 8: Characters optional

As you saw in the previous lesson, the Kleene star and plus allow us to match repeated characters in a line.

Another quantifier that is really common when matching and extracting text is the ? (question mark) metacharacter which denotes optionality. This metacharacter allows you to match either zero or one of the preceding character or group. For example, the pattern ab?c will match either the strings "abc" or "ac" because the b is considered optional.

Similar to the dot metacharacter, the question mark is a special character and you will have to escape it using a slash \? to match a plain question mark character in a string.

In the strings below, notice how the the plurality of the word "file" depends on the number of files found. Try writing a pattern that uses the optionality metacharacter to match only the lines where one or more files were found.

Exercise 8: Matching optional characters
Task Text  
match 1 file found? To be completed
match 2 files found? To be completed
match 24 files found? To be completed
skip No files found. To be completed
Solution

We can use the meta-character '\d' to match the number of files and use the expression \d+ files? found\? to match all the lines where files were found.

Note that the first question mark applies to the preceding 's' character (for plurality), and the actual question mark at the end must be escaped to match the text.

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