

However, the question mark tells the regex engine that failing to match u is acceptable. Then the engine checks whether u matches n. The engine continues, and finds that o matches o, l matches l and another o matches o. The first position where it matches successfully is the c in colonel. The first token in the regex is the literal c. Let’s apply the regular expression colo u ? r to the string The colonel likes the color green. The discussion about the other repetition operators has more details on greedy and lazy quantifiers. turn off the greediness) by putting a second question mark after the first. You can make the question mark lazy (i.e. The effect is that if you apply the regex Feb 23 ( rd ) ? to the string Today is Feb 23rd, 2003, the match is always Feb 23rd and not Feb 23. Only if this causes the entire regular expression to fail, will the engine try ignoring the part the question mark applies to. The engine always tries to match that part. The question mark gives the regex engine two choices: try to match the part the question mark applies to, or do not try to match it.

The question mark is the first metacharacter introduced by this tutorial that is greedy. You can also use curly braces to make something optional. Feb ( ruary ) ? 23 ( rd ) ? matches February 23rd, February 23, Feb 23rd and Feb 23. You can write a regular expression that matches many alternatives by including more than one question mark. E.g.: Nov ( ember ) ? matches Nov and November. You can make several tokens optional by grouping them together using parentheses, and placing the question mark after the closing parenthesis. The question mark is called a quantifier. colo u ? r matches both colour and color. The question mark makes the preceding token in the regular expression optional.
