Regular Expressions Module 1
Foundations: what a regex is and how matching works interview questions
Interview questions on “Foundations: what a regex is and how matching works”: 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
How is a regular expression different from searching for a fixed word?
Answered in the lesson What a regular expression is and where it runs
-
Fresher to mid
Why can the same pattern report a different match position in different languages?
Answered in the lesson What a regular expression is and where it runs
-
Fresher to mid
Which questions do Python's search, match and fullmatch answer, and what do JavaScript's test and Go's MatchString answer?
Answered in the lesson How an engine finds a match, step by step
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
Mid to senior
A teammate validates whole user-entered URLs with one long regular expression; broken URLs slip through and some valid ones are rejected. When is a regex the wrong tool for a job like this, and what would you use instead for a URL, for JSON and for HTML?
- What it tests
- Whether the candidate knows that nested, quoted or recursive formats need a parser rather than a flat pattern, and can name the right tool for each format.
- First step
- Separate what a regex can check from what it cannot. A pattern can test a small fixed shape, but a URL has structure — scheme, host, path, query, percent-encoding — that a flat pattern cannot track, so reach for the platform URL type, a JSON parser and an HTML parser, and keep regex for the small fields inside the parsed result.
- Follow-ups
-
- Where is a regex still the right tool on text like this?
- How would you check one small field, such as a postcode, inside parsed data?
- What risk does a very long validation pattern carry beyond being wrong?
From the lesson What a regular expression is and where it runs
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 highlighter finds a word with Go and gets its byte offset, then uses that number to cut the word out of a JavaScript string in the browser. On text with an accented letter or an emoji the highlight lands on the wrong characters. Explain the bug and fix it.
- What it tests
- Whether the candidate knows match positions are counted in different units — bytes, code points, code units — and keeps one engine's counting from end to end.
- First step
- Name the units. Go returns a byte offset, but a JavaScript string is indexed in UTF-16 code units, so the same number points at a different place once a character needs more than one byte or one unit. Recompute the offset in the target string instead of carrying Go's byte number across to JavaScript.
- Follow-ups
-
- Why does plain ASCII text hide this bug?
- How would you turn a byte offset into a code-unit index safely?
- What breaks in particular with an emoji above U+FFFF?
From the lesson What a regular expression is and where it runs
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
A syntax highlighter finds operators with the pattern <|<=|<<|<<=|=. On the text x <<= 2 it colours <, < and = as three tokens instead of one shift-assign token. Why does it do that, and how do you fix the pattern?
- What it tests
- Applying the leftmost-first rule to alternation, seeing that the order of alternatives is part of the meaning, and fixing it without breaking the other operators.
- First step
- Run the pattern by hand on the text at the position of the first <. 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 a list of operators at start-up, and what must you do to each operator first?
- Would Go's POSIX mode, which is leftmost-longest, give the same result on the original order?
- How do you test that every operator in the table is found whole?
From the lesson How an engine finds a match, step by step
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A Python service and a nightly job that runs grep -E share the unit pattern m|mm|min. On the text 15 min the service extracts m and the job extracts min. Which is right, why do they differ, and how do you make them agree?
- What it tests
- Knowing that two match-selection rules exist, who uses which, and how to write or test patterns that do not depend on the rule.
- First step
- Write down the matches that start at the m of min, then ask which rule picks m and which picks min. Look up what each tool documents before you decide who is wrong.
- Follow-ups
-
- Does min|mm|m give different results in the two tools?
- Can the two rules give different groups even when the whole match is the same?
- What is the safest way to keep two tools in step on a shared pattern?
From the lesson How an engine finds a match, step by step
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
All Regular Expressions interview questions · The Regular Expressions track