Level 3: Advanced · Lesson 26 of 30
Build Your Own Pattern
A repeatable method for writing a new pattern from scratch: collect samples, start literal, generalise piece by piece, add boundaries, and test against near misses.
Writing a regex from nothing can feel like guessing. It becomes much easier with a method. The one below works for almost any format, from order numbers to room codes to times of day.
Step one is to collect samples. Write down at least three strings that must match and three that must not. The negative samples matter most. They should be near misses, such as an ID with one digit too many, the wrong case, or a missing separator. Without near misses you cannot tell whether your pattern is too loose.
Step two is to start literal. Take one positive sample, such as ORD-2026-0042, and write it exactly as a pattern. It will match that one string and nothing else. This gives you a working baseline to change one piece at a time.
Step three is to generalise each piece. Replace the digits with \d and a precise count: ORD-\d{4}-\d{4}. Replace letters with a class such as [A-Z] when the letters vary. Prefer exact counts like {4} or tight ranges like {1,2} over + whenever the format is fixed. Every time you generalise, re-run your samples.
Step four is to add boundaries. Decide whether the value lives inside other text or stands alone. For extraction, wrap the pattern in \b so it does not match inside longer tokens. For validating a whole input, use ^ and $. Boundaries are what reject SKU-123456 when you only wanted five digits.
Step five is to handle ranges with alternation. Some rules cannot be expressed with counts alone. A 24-hour time needs hours from 00 to 23, so the hour becomes (?:[01]\d|2[0-3]) and the minutes become [0-5]\d. Break the range into chunks that each have simple character classes.
Step six is to test and explain. Paste all your samples into the tester, check every match, and read the explanation panel to confirm each token does what you think. Then try a few strings you did not plan for, such as empty values, extra spaces and lowercase letters. If a match surprises you, add that string to your negative samples and tighten the pattern. Your sample list doubles as documentation for whoever maintains the pattern next.
Live tester
2 matches (JavaScript engine)
ORD-2026-0042, ORD-26-42, ORD-2025-1234Examples
Numbers
/\b\d{1,3}(?:,\d{3})+\b/Population 1,234,567 and budget 45,000 and 999
Matches: “1,234,567”, “45,000”
One to three leading digits followed by groups of exactly three describe thousands separators.
Text
/\b[A-Z][a-z]+ Test\b/Ali Test met Sara Test and test user.
Matches: “Ali Test”, “Sara Test”
A capitalised first name followed by a literal surname, built up from one sample.
Codes
/\b[A-Z]{3}\d{2}[A-Z]\b/Codes ABC12D and XY12Z on file.
Matches: “ABC12D”
Each position is generalised separately: three letters, two digits, one letter.
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: Match product codes written as `SKU-` followed by exactly five digits.
SKU-12345 SKU-123 SKU-123456 sku-54321
Task 2: Match room labels like `Room 12B`: one or two digits and one capital letter.
Room 12B, Room 7A, Room 123, room 5C
Task 3: Match valid 24-hour times from `00:00` to `23:59`.
Open 09:30, close 23:59, bad 24:10 and 7:5