Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

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.

12 questions: 4 warm-ups and 8 with model answers · Levels: Fresher to senior

Warm-up questions

Short questions with short answers, each answered in full.

  1. 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?

    Answered in the lesson Capturing and non-capturing groups

  2. Fresher to mid

    Why does the same named-group pattern work in one engine and refuse to compile in another?

    Answered in the lesson Named groups across regex flavours

  3. 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

  4. 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 \1 for 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+\1 on foo bar finds no match even though both words are three letters. A reference in a replacement string, $1 in 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.

  1. Fresher to mid

    A Python loader parses ports=80,443,8080 with ports=(\d+)(?:,(\d+))* and re.fullmatch, then builds the port list from groups 1 and 2. It returns 80 and 8080, losing 443, and raises TypeError on ports=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 for ports=80, where the repeated part runs zero times, and compare what the two lines hand back.
    Follow-ups

    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.

    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
  2. Mid to senior

    A checkout converts rupee amounts to paise with ^(\d+)(?:\.(\d{1,2}))?$, as group 1 × 100 + group 2. For 250 the JavaScript copy computes NaN and the Python copy raises TypeError, and every copy turns 250.5 into 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.75 and 250.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

    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
  3. 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

    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
  4. Mid to senior

    A refactor renamed the year group of a shared date pattern to yyyy and kept the old replacement templates. Now JavaScript and Go write 18/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

    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
  5. 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

    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
  6. 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.search do after an attempt fails? Then compare the ten one-character alternatives with what a character class would say.
    Follow-ups

    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
  7. Mid to senior

    A Go port of the doubled-word check \b(\w+)\s+\1\b matches \b(\w+)\s+(\w+)\b and keeps matches whose groups are equal. Unlike JavaScript, it misses the pairs in a b b and x 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

    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
  8. Mid to senior

    The month-equals-day check must now also read DD/MM/YYYY rows, 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 flags 12//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 \1 point at, and has that group taken part in this attempt?
    Follow-ups

    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

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.