Decide whether your regex should match Unicode characters or ASCII only.
How It Works
By default many string regex constructs such as \w and \d recognize Unicode categories. re.ASCII narrows these classes and related word boundaries to ASCII behavior. For human-language text, Unicode often matters; for fixed technical identifiers, ASCII can be clearer.
Normalization and casefold() address separate Unicode concerns from matching classes.
Step-by-Step Example
import re
sample = "café42"
print(re.findall(r"\w+", sample))
print(re.findall(r"\w+", sample, flags=re.ASCII))
Understanding the Example
- The expression '"café42"' supplies the value stored as 'sample'. This value is then used or checked by the later statements.
- The print() call exposes the result so you can compare it with the behavior described above. Check the value and its type when experimenting.
A successful run of this example prints the following output:
['café42']
['caf', '42']
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
Compare the two modes on Greek letters, accented names and Arabic-Indic digits.
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
- Assuming \w always means only letters A–Z and digits 0–9.
- Enabling re.ASCII for international names and unintentionally breaking valid input.
Key Takeaways
- Decide whether your regex should match Unicode characters or ASCII only.
- By default many string regex constructs such as \w and \d recognize Unicode categories.
- Normalization and casefold() address separate Unicode concerns from matching classes.