Matching
Usernames
Some institutions issue usernames that follow the pattern “One or more alphabetic characters, followed by one or more digits.” Which of the following regular expressions would match (only) valid usernames under that policy? You can assume that usernames are case insensitive, and that your regular expression only needs to match their lowercase form.
| ☐ | \w\d | ☐ | \w+\d+ | |
| ☐ | [a-z]+\d+ | ☐ | [a-z*\d+ | |
| ☐ | ^[a-z]+\d+$ | ☐ | ^[a-z]*\d+$ |
If an institution didn’t want to allow arbitrarily long usernames but, instead, only wanted to accept usernames with five or six alphabetic characters followed by one or two digits, how would your regular expression need to change?
Ook!
Ook! is an esoteric language with a limited set of legal
instructions. Those commands can (with some simplification) be described by the regular
expression ^(Ook\?(?=Ook[.!]))|(Ook[.!])Ook[.!?]$
Based on the above definition, which of the following are valid Ook! instructions?
| ☐ | Ook. | ☐ | Ook.Ook. | |
| ☐ | Ook.Ook! | ☐ | Ook!Ook. | |
| ☐ | Ook?Ook! | ☐ | Ook?Ook? | |
| ☐ | OokOok | ☐ | OokOokOok | |
| ☐ | Ook.Ook.Ook. | ☐ | Ook?Ook!Ook.Ook! |
Writing regular expressions
In this part, you will write a regular expression to capture (a slightly simplified definition of) valid email addresses.
Usernames
Usernames in email addresses can only contain the following characters:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789
._-
In addition, the following rules hold:
- A period ‘
.’ cannot be the first or last character - Two consecutive periods are not allowed.
Write a regular expression to capture valid email usernames:
Domain names
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789-.
- The first and last characters can not be a dash ‘
-’ or period ‘.’ - Two consecutive periods are not allowed.
- The top-level (right-most) domain component must be a period followed by a 2- or 3-character lower-case alphabetic string.
Write a regular expresion to capture valid email domain names:
Combined regex
Write a combined regular expression to represent valid email addresses under our definition:
Invent your own
Write your own regular expression:
Describe your regular expression in English:
Fold your paper over so that only your English description is visible, and pass it to your neighbor. Have them try to write a regular expression capturing your description: