Advanced Lesson 1
Regular Expressions
Chapter 5: Exercises
Sample answers
Here are sample answers for the exercises in this chapter. As usual, there might be other valid answers. I’ve purposely kept this away from you so that you try the questions first!
Task 1: Interpreting regular expressions
No solutions provided! I’m sure you have interpreted the regular expressions correctly! 😈
Task 2: Writing regular expressions
^[A-Za-z ]*$^[a-z ]*b$^(a([a-z]a)*)?$^(b+(ab+)*)?$
Task 3: Building parsers
^[A-Za-z_](\w)*$^[A-Za-z_](\w)* ?= ?[+-]?\d+(.\d*)?$^([A-Za-z_](\w)*|\d+) ?(==|!=|<=?|>=?) ?([A-Za-z_](\w)*|\d+)$
Task 4: Email parser
- The
USERpart:^[0-9a-zA-Z_.+?-]+$ - The
HOSTpart:^[0-9A-Za-z-]*[A-Za-z][0-9A-Za-z]*$ - The
EXTpart:^(com?|org)$ - Combined:
[0-9a-zA-Z_.+?-]+@[0-9A-Za-z-]*[A-Za-z][0-9A-Za-z]\.(com?|org)
There are obviously many different possible solutions to this one!