Advanced Lesson 1
Regular Expressions
Chapter 2: Regular expression basics
Match any character
Ok, time to spice things up!
Let’s say instead of just "bop"
, you want to match any three letter words that start with a b
and ends with a p
.
So you want to be able to match the following strings: bop
, bap
, bmp
, bbp
, b2p
, b#p
, b&p
, b!p
, and b@p
.
How would you write a regular expression for this?
You will use a special .
character. The .
represents any character except a new line.
>>> re.match("b.p", "bop")
<re.Match object; span=(0, 3), match='bop'>
>>> re.match("b.p", "b2p")
<re.Match object; span=(0, 3), match='b2p'>
>>> re.match("b.p", "b!p")
<re.Match object; span=(0, 3), match='b!p'>
If you need to match a full stop (.
), you should escape it with a backslash (\
).
>>> re.match("b.p\.", "bop.")
<re.Match object; span=(0, 4), match='bop.'>
Quick task
Write a regular expression to match all strings that have an e
as its second and fourth letters.
Example valid strings: hehe
, were
, beke
, tepe
, eeee
aeae
.
Example invalid strings: eeaa
, abcd
, hello
, care
.
Try this - this should be a quick one!
>>> pattern = ".e.e"
>>> re.match(pattern, "were")
<re.Match object; span=(0, 4), match='were'>
>>> re.match(pattern, "care") # None
>>>