Make the previous thing optional
? means "the previous element, 0 or 1 times." In other words: optional. colou?r matches both color and colour — the u can be there or not.
This is a hugely common pattern. Half the spelling variants in the world are handled with one well-placed ?.
Common uses
- Optional protocol:
https?://example.commatches both http:// and https:// versions. - Optional sign:
-?\d+matches signed or unsigned integers. - Optional country code:
(\+1)? \d{3}-\d{4} - British vs American spelling:
analy[sz]e,colou?r,cent(er|re).
? has a SECOND meaning (preview of lesson 7)
Ahead-of-time warning: when ? appears immediately AFTER another quantifier, it changes meaning. *?, +?, ??, {n,m}? are all lazy quantifiers (covered in lesson 7). The same character has two roles depending on context. Don't confuse x? (optional x) with x*? (lazy 0-or-more).