Advanced Lesson 1
Regular Expressions
Chapter 3: Quantifiers
Optional
Color
or Colour
?
Flavor
or Flavour
?
Favorite
or Favourite
?
Why does English have to be so complicated?
Well, with regular expressions, you can capture both spellings!
>>> pattern = "colou?r"
>>> re.match(pattern, "color")
<re.Match object; span=(0, 5), match='color'>
>>> re.match(pattern, "colour")
<re.Match object; span=(0, 6), match='colour'>
The question mark ?
represents “zero or once”. It basically means optional.
Quick task
Write a regular expression that can match these strings: 0 file found
, 1 file found
, 2 files found
, 13 files found
. The first word should be a number (can be more than 1 digits), the second word should be file
or files
, and the third word is always found
.
1 files found
and 2 file found
are also valid. Regular expressions will not be able to handle such cases because they have no ‘memory’ to remember the characters they have previously seen.
>>> pattern = "[0-9]+ files? found"
>>> re.match(pattern, "1 file found")
<re.Match object; span=(0, 12), match='1 file found'>
>>> re.match(pattern, "200 files found")
<re.Match object; span=(0, 15), match='200 files found'>