← All Python interview questions
Extract order IDs from support ticket text
EasyStrings & Text CleaningZohoFreshworks
Support tickets mention order IDs in free text, always in the form ORD- followed by exactly 5 digits.
Write extract_ids(tickets) that takes a list of ticket strings and returns a list of the first order ID found in each ticket, or None when a ticket contains no valid ID.
This fixture runs before your code on every Run and Submit, so the data is always clean.
import re
tickets = [
"Refund pending for ORD-10234 since Monday",
"no order reference here",
"Duplicate charge on ORD-98765 and ORD-11111",
"typo ORD-123 should not match",
]- Step 1: `re.compile` builds the pattern once instead of re-parsing it for every ticket — it matters once you are scanning millions of rows.
- Step 2: `\d{5}` pins the length to exactly five digits, so the malformed `ORD-123` is correctly ignored.
- Step 3: `search` finds the first match anywhere in the string; `match` would only look at the start.
- Step 4: Returning `None` rather than an empty string keeps 'no ID' distinguishable from 'blank ID' downstream.
def extract_ids(tickets):
pattern = re.compile(r"ORD-\d{5}")
out = []
for text in tickets:
match = pattern.search(text)
out.append(match.group(0) if match else None)
return outLearn the concept first
Stuck on Strings & Text Cleaning? These free Python lessons teach it step by step.
More Strings & Text Cleaning questions
Asked at Zoho
Practising alone only goes so far — our live cohorts pair Python and Pandas depth with dashboards, agentic analytics projects and mock interviews.
Explore the program