Natural Language Processing · TEE · read

Six full regex questions and solutions

Regular expressions: read, build and test text patterns

TEE · current Unit 3 syllabus and supplied Regular Expressions handout; outside confirmed M2 page cutoff

Question. From “cat catalog wildcat Cat”, extract only the lowercase whole word cat using Python regex.

Given

Case-sensitive matching; whole words only.

To find

A pattern and its matches.

Rule and why it applies

Use \b boundaries around the literal word. This prevents substring matches inside longer word tokens.

Step 1. Write the literal

cat alone would also match catalog and wildcat.

Step 2. Add both word boundaries

r"\bcat\b".

Step 3. Check candidates

cat matches. catalog fails at the ending boundary. wildcat fails at the starting boundary. Cat fails because matching is case sensitive.

Step 4. Extract

re.findall(r"\bcat\b", text) returns ["cat"].

Answer. Pattern \bcat\b; one match: cat. With IGNORECASE, Cat would also match.

Question. Write a regex that validates exactly ten ASCII digits, then test 9876543210, 98765 and A9876543210.

Given

No country-prefix restriction; digits only; entire input must match.

To find

A validation pattern and each result.

Rule and why it applies

[0-9] permits one ASCII digit; {10} repeats it exactly ten times; fullmatch prevents extra characters.

Step 1. Choose the character class

[0-9].

Step 2. Specify exact length

[0-9]{10}.

Step 3. Require the whole input

re.fullmatch(r"[0-9]{10}", text). In a simple single-line exam pattern, ^[0-9]{10}$ expresses the intended format.

Step 4. Test all examples

9876543210: 10 digits → valid. 98765: only 5 → invalid. A9876543210: contains an extra letter → invalid.

Answer. Use fullmatch with [0-9]{10}. Only 9876543210 passes. This checks format, not whether a phone number exists.

Question. Extract date-shaped strings in DD/MM/YYYY format from “On 05/10/2026 and 7/10/2026 we met.” Require two digits for day and month and four for year.

Given

Format recognition only; no calendar-validity test.

To find

Pattern and extracted strings.

Rule and why it applies

Use fixed repetition counts separated by literal slashes, and boundaries around the full pattern.

Step 1. Day part

[0-9]{2}.

Step 2. Month and year

[0-9]{2}/[0-9]{2}/[0-9]{4}.

Step 3. Add boundaries

r"\b[0-9]{2}/[0-9]{2}/[0-9]{4}\b".

Step 4. Evaluate the two candidates

05/10/2026 has 2/2/4 digits and matches. 7/10/2026 has a one-digit day and fails.

Answer. Only 05/10/2026 is extracted. A date such as 99/99/2026 has the right shape but needs a calendar parser to reject it semantically.

Question. Use a regex to check a simple email shape: one or more username characters from letters, digits, dot, underscore, percent, plus or hyphen; @; a domain of letters, digits, dots or hyphens; a literal dot; a letter-only suffix of length at least two.

Given

This is the simplified handout format, not full standards-compliant validation.

To find

The pattern and an explanation of every part.

Rule and why it applies

Build the requested components and use fullmatch so no extra prefix or suffix is accepted.

Step 1. Username

[A-Za-z0-9._%+-]+: one or more allowed username characters. The hyphen is last, so it is literal.

Step 2. Separator and domain

@[A-Za-z0-9.-]+: literal @ followed by domain characters.

Step 3. Suffix

\.[A-Za-z]{2,}: escaped dot, then at least two letters.

Step 4. Combine and test

re.fullmatch(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}", text). riya+nlp@example.com matches; riya.example.com lacks @ and fails.

Answer. The combined pattern meets the stated classroom shape. It neither proves address existence nor enforces every real email/domain constraint.

Question. For text “The rain in Spain”, write patterns that test starts with The, ends with Spain, and satisfies both.

Given

Single-line, case-sensitive text.

To find

Three patterns and outcomes.

Rule and why it applies

Use ^ for beginning, $ for ending, and .* for the intervening sequence in this simple one-line task.

Step 1. Starts with

^The succeeds.

Step 2. Ends with

Spain$ succeeds.

Step 3. Both

^The.*Spain$ succeeds because the text begins with The and ends with Spain.

Step 4. Why dot-star?

. consumes one intervening character; * allows zero or more, including spaces here.

Answer. All three checks succeed. Without anchors a substring match alone would not enforce the required position.

Question. Construct a case-sensitive pattern for full filenames ending in .jpg or .png and test photo.jpg, chart.png and photo.jpg.exe.

Given

Filename must have at least one character before the dot. Treat the filename as a single line.

To find

Pattern and three results.

Rule and why it applies

Use a noncapturing alternative for the two complete suffixes and fullmatch to disallow trailing text.

Step 1. Filename stem

.+ requires one or more characters.

Step 2. Literal dot

\..

Step 3. Choose suffix

(?:jpg|png) chooses jpg or png.

Step 4. Full match and test

re.fullmatch(r".+\.(?:jpg|png)", filename). photo.jpg passes; chart.png passes; photo.jpg.exe fails.

Answer. Full pattern .+\.(?:jpg|png); use IGNORECASE only if uppercase extensions should also be accepted.

Pattern likhke ek valid, ek short/wrong, ek extra-character case zaroor mentally run karo.

Regular expressions: build and explain exact text patterns

id
tee-08
title
Regular expressions: build and explain exact text patterns
title_confirmed
false
topic_title
Regular expressions: build and explain exact text patterns
mode
tee
kind
focused
status
not_generated
duration
reviewed
false
notebook_url
https://notebook.google.com/notebook/6c2636fe-3511-4542-b47f-ab83795c82cd
youtube_url
corrected_visuals
false
upload_pending_correction
false
correction_note
related_topic_ids

    These are recorded generation statuses, not a live service check. Private YouTube and signed-in NotebookLM links require the owner's account. This export publishes existing catalog metadata, not private video files or an invented transcript.