Technical skill practice
Python Regular Expression: Validate a Complete Identifier
For a Python regular expression that must validate an entire value, use re.fullmatch rather than assuming a substring match validates the whole string. Define a restricted format first. The example accepts two uppercase ASCII letters, a hyphen and exactly three ASCII digits.
Start with a small, explicit task
Validate fictional equipment labels such as QX-407. Reject extra surrounding text, lowercase letters and an incorrect digit count. The pattern is intentionally a small business rule for synthetic labels; it is not a universal identifier, email or security-validation expression.
Run the complete example
This original example was checked with Python 3.12.14. Save it as python_regular_expression.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.
Expected output
[True, False, False, False]
substring found: QX-407
import re
pattern = re.compile(r"[A-Z]{2}-[0-9]{3}")
labels = ["QX-407", "qx-407", "QX-40", "prefix QX-407 suffix"]
valid = [pattern.fullmatch(label) is not None for label in labels]
assert valid == [True, False, False, False]
assert pattern.search(labels[-1]) is not None
assert pattern.fullmatch("QX-407\n") is None
print(valid)
print("substring found:", pattern.search(labels[-1]).group())
Explain the result
The first character class limits the two-letter prefix to A through Z. The digit class limits the numeric part to 0 through 9, and the braces specify exact counts. A hyphen outside a class is a literal separator here. The raw-string prefix is a useful habit for regex patterns because backslashes then reach the regex parser without an extra layer of ordinary string escaping.
The long fourth label fails fullmatch but succeeds with search because it contains a valid-looking fragment. Those results answer different questions. The final newline check also fails whole-value validation. If surrounding whitespace should be allowed, make normalization an explicit step and decide which characters may be removed before validating.
Avoid these mistakes
Do not replace [0-9] with a broader shorthand without checking whether the accepted character set changes. Avoid adding a permissive wildcard merely to make rejected examples pass. When a literal split or prefix check fully answers the task, a regular expression may be unnecessary.
Questions, answered
Why not use search for validation?
search can find a matching fragment inside unrelated surrounding text, as the fourth label shows.
Does this accept lowercase labels?
No. The stated format and checked character class accept uppercase ASCII letters only.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.