Final exam
Regular Expressions
Before you start
- 20 questions, drawn at random from a pool of 34.
- Pass mark: 70%. Your final score is 30% practice + 70% exam.
- No time limit, but finish within 3 hours. You can move between questions before you submit.
- Failed? Review the lessons and try again: every attempt gets a new set of questions.
Question 1 of 20Choose one
Which pattern matches the literal text "1+1=2"?
Question 2 of 20True or false
Without any flags, the pattern Error matches the text "ERROR: disk full".
Question 3 of 20Sort into groups
Does each string contain a match for gr[ae]y?
Tap an item, then tap its group (or drag it there).
Question 4 of 20Match the pairs
Match each shorthand to its meaning.
Tap an answer, then tap the place where it belongs (or drag it there).
\d\S\w[^\d]
Question 5 of 20Choose one
What does the class [^aeiou] match?
Question 6 of 20Fill in the blanks
Fill in the blanks
The quantifier that means "zero or one time", making the previous item optional, is the .
Question 7 of 20Work it out
How many different strings does the pattern [0-9]{4} match, from 0000 to 9999?
Question 8 of 20Choose one
Applied to "<p>Hi</p>", what does the lazy pattern <.+?> match first?
Question 9 of 20What would you do?
What would you do?
A developer validates order IDs with [A-Z]{3}-\d{5} and finds that "XXABC-12345678" passes. What is the fix?
Question 10 of 20Work it out
A text has 5 lines, and 3 of them start with "#". How many matches does /^#/gm find?
Question 11 of 20Sort into groups
Does \bpro\b find a match in each text?
Tap an item, then tap its group (or drag it there).
Question 12 of 20Choose one
Which pattern correctly tests that a whole string is exactly "yes" or "no"?
Question 13 of 20Work it out
In the pattern ((\d{3})-(\d{3}))-(\d{4}), which group number captures the last four digits?
Question 14 of 20True or false
The pattern (\w)\1 matches "aa" and "77" but not "ab".
Question 15 of 20Choose one
In JavaScript, what does "Lee, Ana".replace(/(\w+), (\w+)/, "$2 $1") return?
Question 16 of 20Fill in the blanks
Fill in the blanks
A group written with the syntax (?<year>...) is called a , and in JavaScript its value is read from match.groups.year.
Question 17 of 20Spot the mistake
A developer wants to convert every "colour" to "color" in a document with JavaScript. Tap the line that explains why only the first one changed.
Question 18 of 20Choose one
Why does new RegExp("\bcat\b") fail to match "the cat sat" in JavaScript?
Question 19 of 20Match the pairs
Match each JavaScript call to what it returns.
Tap an answer, then tap the place where it belongs (or drag it there).
/\d/.test("a1")"a1b2".match(/\d/g)"x".match(/\d/)"a1b2".split(/\d/)
Question 20 of 20Choose one
Which Python function requires the entire string to match the pattern?
Question 21 of 20True or false
In Python, re.findall(r"(\d+)-(\d+)", "1-2 and 3-4") returns a list of tuples.
Question 22 of 20Choose one
Which grep option enables extended regex syntax, where +, ? and | work without backslashes?
Question 23 of 20Choose one
Which pattern matches a number only when it is followed by "kg", without including "kg" in the match?
Question 24 of 20Sort into groups
Does each password pass ^(?=.*[a-z])(?=.*\d).{6,}$?
Tap an item, then tap its group (or drag it there).
Question 25 of 20True or false
Lookarounds consume the characters they check, so those characters become part of the match.
Question 26 of 20Choose one
Applied to "a","b", what does the greedy pattern ".*" match?
Question 27 of 20Select all that apply
Which changes reduce the risk of catastrophic backtracking? Select all that apply.
Select all that apply.
Question 28 of 20Work it out
A pattern's backtracking work doubles with every extra input character. If an input of 30 characters takes 1 second to fail, roughly how many seconds does an input of 40 characters take?
Question 29 of 20What would you do?
What would you do?
A team must extract all links from thousands of HTML pages with deeply nested markup. A developer proposes one large regex. What is the best advice?
Question 30 of 20Choose one
Why is ^[^\s@]+@[^\s@]+\.[^\s@]+$ a reasonable email check for a sign-up form?
Question 31 of 20Spot the mistake
Tap the two inputs that the IPv4 octet pattern ^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$ rejects.
Question 32 of 20Choose one
In JavaScript, which pattern matches a word made of letters from any language, such as "Zoë" or "Μαρία"?
Question 33 of 20Put in order
Order these steps for safely changing a regex that is already used in production.
Drag the items, or use the arrow buttons, to put them in order.
- Run the tests and check for backtracking risks
- Modify the pattern
- Collect real examples that should and should not match
- Deploy the change
- Add them as automated tests and confirm the current pattern's behavior
Question 34 of 20True or false