Use literal string processing, regex and Unicode normalization in one exercise.
How It Works
Choose the simplest operation for each task: strip outer whitespace, normalize equivalent Unicode text, and use a regex only for a genuinely patterned token. This checkpoint is designed to exercise three distinct decisions rather than one complicated pattern.
Keep the parsed identifier and display name as separate data fields, with different validation policies.
Step-by-Step Example
import re
import unicodedata
raw = " ID-42 | Jose\u0301 "
clean = unicodedata.normalize("NFC", raw.strip())
code, _, display_name = clean.partition("|")
match = re.fullmatch(r"ID-[0-9]+", code.strip())
print(bool(match), display_name.strip())
Understanding the Example
- The expression '" ID-42 | Jose\\u0301 "' supplies the value stored as 'raw'. This value is then used or checked by the later statements.
- The expression 'unicodedata.normalize("NFC", raw.strip())' supplies the value stored as 'clean'. This value is then used or checked by the later statements.
A successful run of this example prints the following output:
True José
Where This Fits in a Real Program
Start by writing down examples that should match and examples that must fail. Check whether you need to locate a substring, match a prefix or validate an entire field; these are different operations. Unicode and escape sequences can also change which characters are matched, so test accented text and empty input.
Practice: Try It Yourself
Reject malformed identifiers while keeping accented display names intact.
For this exercise, predict the result before running the changed code. Then explain the difference between the original and modified cases using the actual values, not just whether the program completed.
Further Reading
Read the official Python documentation for complete parameters, return types and exceptions.
Common Beginner Mistakes
- Using .replace() as a general security filter when validation is required.
- Applying an ASCII-only regex indiscriminately to international human names.
Key Takeaways
- Use literal string processing, regex and Unicode normalization in one exercise.
- Choose the simplest operation for each task: strip outer whitespace, normalize equivalent Unicode text, and use a regex only for a genuinely patterned token.
- Keep the parsed identifier and display name as separate data fields, with different validation policies.