Level 1: Foundations · Lesson 10 of 30
Mini project: extract codes
Put the first nine lessons together to extract order codes like ORD-1001 from messy text, while skipping lookalikes and broken codes.
Time to combine what you have learned. A common real task is pulling codes out of text: order numbers in emails, ticket IDs in logs, or product SKUs in a list. In this project the codes look like ORD-1001: a prefix of capital letters, a hyphen, and one or more digits.
Start with the simplest pattern that works on a clean example. ORD-\d+ matches ORD-1001. It uses literal text for the prefix and hyphen, and the digit shorthand with a plus for the number. Always test the happy path first, then look for ways the pattern goes wrong.
Now add tricky input. In XORD-5, the simple pattern finds ORD-5 inside a longer token, which is a false match. Adding a word boundary in front, \bORD-\d+, fixes it, because there is no boundary between X and O. Adding a boundary at the end, \bORD-\d+\b, also stops a code from matching when letters are glued to the digits, as in ORD-12abc.
Next, generalise the prefix. If several prefixes are valid, replace the literal with a character set: \b[A-Z]+-\d+\b matches AB-12, ABC-456 and A-7, while lowercase abc-3 and the hyphen-less AB12 are skipped. Case matters here, so think about whether lowercase codes should count before you add the i flag.
Sometimes position matters too. If valid ticket codes only appear at the start of a line, use the start anchor with multiline mode: ^TKT-\d+ with the m flag. A code mentioned in the middle of a sentence, like see TKT-2, is then ignored.
Finally, use groups if you need the parts. \b([A-Z]+)-(\d+)\b captures the prefix and the number separately, so your code can count orders per prefix or sort by number. In JavaScript, text.matchAll(re) gives you every match with its groups; Python offers re.finditer, PHP preg_match_all, and Go FindAllStringSubmatch.
Remember that a regex checks shape only. ORD-99999999 has the right shape even if no such order exists. Treat extracted codes as candidates, and confirm them against your real data when it matters.
Live tester
4 matches (JavaScript engine)
Orders ORD-1001, ORD-1002 and REF-77 shipped; XORD-5 is noise.
Examples
Numbers
/\b\d+\b/gShipped 12 boxes, 300 items, code A7
Matches: “12”, “300”
Boundaries keep the digit in A7 from being treated as a standalone number.
Text
/\b[A-Z][a-z]+\b/gAli Test met Sara Demo at noon
Matches: “Ali”, “Test”, “Sara”, “Demo”
A capital letter followed by lowercase letters picks out the fictional names.
Codes
/\bSKU-\d+\b/gSKU-100, SKU-2050 and XSKU-9 in stock
Matches: “SKU-100”, “SKU-2050”
The leading boundary rejects the lookalike XSKU-9.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Extract valid ORD codes: uppercase ORD, a hyphen and digits, as a standalone token.
ORD-1001, ORD-22, XORD-5, ORD-ABC, ord-9
Task 2: Extract codes made of uppercase letters, a hyphen and digits, with any prefix length.
AB-12 abc-3 ABC-456 A-7 AB12
Task 3: Extract TKT codes only when they start a line.
TKT-1 open see TKT-2 TKT-3 closed