Advanced Lesson 1
Regular Expressions
Chapter 5: Exercises
Summary
Ok, I have provided you with the basics of regular expressions. Hopefully are able to write your own regular expressions by now!
As a recap, here are the special metacharacters for regular expressions in Python.
.
: Match any characters except a newline.?
: Match zero or one repetitions*
: Match zero or more repetitions+
: Match one or more repetitions[
]
: Match a set of characters(
)
: Grouping|
: Or{m}
: Match exactlym
repetitions{m,n}
: Matchm
ton
repetitions. Omittingn
will give you infinity.^
: Match the start of a string, or “not” if used inside[ ]
.$
: Match the end of a string\b
: Word boundary marker. Remember to use raw stringsr"\b"
or escape it"\\b"
when using this in Python.\
: Escape characters. Use this to represent any of the metacharacters above (e.g."\?"
if you want to match a question mark). Escaping is not needed inside square brackets.