Regular Expressions Module 5
Groups, alternation and backreferences interview questions
Interview questions on groups, alternation and backreferences: warm-ups, then what each harder question tests and how a good answer starts.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher to mid
In the match of (\d{4})-(\d{2})-(\d{2}), what does group 0 hold and how are the other groups numbered?
-
Fresher to mid
Why does the same named-group pattern work in one engine and refuse to compile in another?
-
Fresher to mid
What does | do, and why does ^cat|dog$ accept "hotdog"?
The bar is the alternation operator. It is the lowest-precedence operator of a pattern, so it binds at the top level and splits the whole pattern into two alternatives, one on each side. The engine tries the alternatives in the order the pattern lists them and keeps the first that fits at the leftmost position. In ^cat|dog$ that means (^cat)|(dog$), two alternatives: the start of the text followed by c, a, t, or d, o, g followed by the end. On "hotdog" the first alternative fails at the h, the second finds d, o, g at the end, and the whole pattern matches. Wrap both alternatives in a non-capturing group, ^(?:cat|dog)$, and the anchors now sit around both: the pattern accepts "cat" and "dog" only.
From the lesson Alternation and the order of choices
-
Fresher to mid
What is a backreference, and how is it different from a reference to a captured group in a replacement string?
A backreference is a pattern element that names a previously captured group inside the same pattern and matches exactly the text that group holds at that point. Usual spellings are
\1for the first group,\k<name>for a named group in V8 and PCRE2, and(?P=name)in CPython and PCRE2. It runs against the subject text and may fail, so(\w+)\s+\1onfoo barfinds no match even though both words are three letters. A reference in a replacement string,$1in JavaScript and PHP or\g<name>in Python, runs after the search is done: it reads the captured text and puts it in the output, which cannot fail. Each sits in a different half of the search-and-replace cycle.From the lesson Backreferences: matching the same text again
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
Fresher to mid
A Python loader parses
ports=80,443,8080withports=(\d+)(?:,(\d+))*andre.fullmatch, then builds the port list from groups 1 and 2. It returns 80 and 8080, losing 443, and raises TypeError onports=80. Explain both results and give two ways to get every port.- What it tests
- Whether the candidate sees what a group inside a repeated part holds after several runs and after none, and can collect a list of values without writing one group per value.
- First step
- Write down what group 2 holds after each run of the repeated part on
ports=80,443,8080. Then do the same forports=80, where the repeated part runs zero times, and compare what the two lines hand back. - Follow-ups
-
- Why does adding more optional groups, one per port, only move the problem?
- How do you keep the strict check of the whole line and still get every port?
- How would you report which entry is wrong on a line such as
ports=80,abc,443?
From the lesson Capturing and non-capturing groups
Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.Opening the model answers with your pass…
Model answers open with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; nothing renews unless you choose to. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A checkout converts rupee amounts to paise with
^(\d+)(?:\.(\d{1,2}))?$, as group 1 × 100 + group 2. For250the JavaScript copy computes NaN and the Python copy raises TypeError, and every copy turns250.5into 25005. Explain each failure and fix the parser in JavaScript, Python, Go and PHP.- What it tests
- Whether the candidate knows how four engines report a group that took no part, spots the second bug that has nothing to do with groups, and keeps money in whole paise without a float on the way.
- First step
- Run the pattern by hand on
250,250.75and250.5, and write down what group 2 holds each time and what that text means in paise. Then look up, engine by engine, what reading a group that took no part returns. - Follow-ups
-
- Why does the Go copy charge 250 rupees correctly, and why is that still a bug?
- What does PHP put in the match array for
250, with and without PREG_UNMATCHED_AS_NULL? - Why not parse the amount as a float and multiply it by 100?
From the lesson Capturing and non-capturing groups
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 4 minutes
-
Mid to senior
A Python batch validates records with named-group patterns, and a nightly browser job pastes the same patterns into JavaScript's RegExp constructor. Half of them raise SyntaxError, and some that compile match different text than Python does. What do you check first, and how do you keep both services in step with one pattern store?
- What it tests
- Whether the candidate knows the two named-group forms and which engines read each, catches cross-flavour traps around classes and line endings, and can set up one pattern store that stays correct in both engines.
- First step
- Open one failing pattern in both engines and read the error each gives. Separate the spelling of the group names from everything else in the pattern, and list which spellings each engine accepts.
- Follow-ups
-
- What other named-group shapes refuse to compile only on one of the two engines?
- How do you catch a case where both engines compile the pattern and match different text?
- What is a safe way to store one pattern and serve both languages from it?
From the lesson Named groups across regex flavours
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 4 minutes
-
Mid to senior
A refactor renamed the
yeargroup of a shared date pattern toyyyyand kept the old replacement templates. Now JavaScript and Go write18/10/without an error, PHP's callback logs a warning, and Python fails on its first call. Explain each behaviour, and how you would catch the next rename before release.- What it tests
- Whether the candidate knows how each engine treats a template name the pattern no longer defines, and can add a check that ties every template to the pattern's group names before release.
- First step
- List the group names the pattern defines after the rename and the names each template asks for, and mark the one that is gone. Then find, engine by engine, what happens to a reference to a missing name, and whether it happens when the template is read or only when a match is replaced.
- Follow-ups
-
- What would JavaScript write if the refactor had removed every name and kept numbered groups?
- Which of these behaviours would you want everywhere, and how do you get it in JavaScript, Go and PHP?
- What test in continuous integration would have caught the rename on the day it was made?
From the lesson Named groups across regex flavours
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 4 minutes
-
Mid to senior
A syntax highlighter paints operators by matching this pattern against each line: <|<=|<<|<<=|=. On the text "x <<= 2" it paints <, < and = as three tokens instead of one shift-assign operator. Explain, then fix the pattern without changing the operator table.
- What it tests
- Applying the leftmost-first rule to a disjunction, seeing that the order of alternatives is part of the pattern, and building the pattern from the table so every entry keeps its meaning.
- First step
- Run the pattern by hand at position 2 of "x <<= 2". List which alternatives can match there and in which order the engine tries them, then note which one it stops at.
- Follow-ups
-
- How do you build the pattern from the operator table at run time, and what sort order do you use?
- Would Go's POSIX engine, which is leftmost-longest, give the right answer on the original order?
- A later entry adds >>= to the table. What breaks if you add it at the end?
- How do you test that every operator in the table is painted as a whole token?
From the lesson Alternation and the order of choices
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 4 minutes
-
Mid to senior
A Python validator meant to accept a field of digits only runs
re.search(r"(0|1|2|3|4|5|6|7|8|9)+$", field). A 2-megabyte field that ends in a letter makes the request time out. Explain the two reasons the pattern is slow, and give a rewrite that accepts digits only and runs in linear time.- What it tests
- Whether the candidate can find where a slow pattern spends its time, tell a real cost from a cosmetic one, and rewrite the validator so that it accepts digits only and runs in linear time.
- First step
- Look at the anchors first: which ends of the field does the pattern tie down, and what does
re.searchdo after an attempt fails? Then compare the ten one-character alternatives with what a character class would say. - Follow-ups
-
- Which pattern is the rewrite, and does it change what the validator accepts?
- Why is an alternation of a character and its repetition, such as (a|aa)+, worse than either alone?
- The Python stack has
reand the third-partyregexmodule. Which one lets a request abort cleanly, and how? - How do you write a regression test that would have caught the slowness on a short input in CI?
From the lesson Alternation and the order of choices
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 5 minutes
-
Mid to senior
A Go port of the doubled-word check
\b(\w+)\s+\1\bmatches\b(\w+)\s+(\w+)\band keeps matches whose groups are equal. Unlike JavaScript, it misses the pairs ina b bandx y y z z, and a pair split by a no-break space. Explain each miss and fix the port.- What it tests
- Whether the candidate can trace a regex-plus-code port against the original pattern case by case, explain each difference from the engines' documented rules, and rebuild the port so that both find the same pairs.
- First step
- Trace the Go pattern by hand on
a b b: write down its first match, where the next search starts and what is left for it. Do the same with the JavaScript pattern, then compare the two traces before you look at white space. - Follow-ups
-
- Is Go's
unicode.IsSpacethe same set of characters as JavaScript's\s? - How many pairs should the port report for
the the the, and why? - How would you show that the two checks agree before the Go worker replaces the old one?
- Is Go's
From the lesson Backreferences: matching the same text again
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 4 minutes
-
Mid to senior
The month-equals-day check must now also read
DD/MM/YYYYrows, so a teammate writes\b\d{4}-(\d{2})-\1\b|\b(\d{2})/\1/\d{4}\b. CPython never flags a slash date, and V8 flags12//2025, which is no date. Explain both, and fix the pattern for CPython, V8 and PCRE2.- What it tests
- Whether the candidate can explain two different wrong results of one pattern from each engine's documented rules, and repair the pattern so that it behaves the same in CPython, V8 and PCRE2.
- First step
- Number the groups of the pattern from the left. Then trace the second alternative by hand on
10/10/2025: which group does its\1point at, and has that group taken part in this attempt? - Follow-ups
-
- Why does V8 accept
12//2025, a row that holds no second number at all? - Which impossible dates does the fixed pattern still flag, and what do you check after it?
- How would named groups or a branch reset remove the numbering trap, and which engines accept each?
- Why does V8 accept
From the lesson Backreferences: matching the same text again
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 4 minutes
All Regular Expressions interview questions · The Regular Expressions track