How to Check a String Against a Pattern in pydantic with Field(pattern=...)
Some text has a fixed shape: a product code such as BIK-1001, a postcode, a 64-character checksum. pydantic checks that shape for you when the field is declared with Field(pattern=...) and a regular expression. This guide does it with pydantic 2.14.4 on Python 3.15.2.
Syntax
from pydantic import BaseModel, Field
class <Model>(BaseModel):
<field>: str = Field(pattern=r"^<regular expression>$")
| Part | What it means |
|---|---|
Field(pattern=...) | Adds a check to the field: its text must match the regular expression |
r"..." | A raw string, so backslashes such as the one in \d reach the regular expression unchanged |
^ and $ | The start and the end of the text. Without them, the pattern only has to match somewhere inside it |
pydantic's Field reference describes pattern as a "Pattern for strings (a regular expression)."
The Example
A shop's product codes are three capital letters, a dash and four digits, such as BIK-1001. A Product model checks a good code and then one written in lower case.
Prerequisites
- pydantic in your project, for example with
uv add pydantic. This guide uses pydantic 2.14.4 on Python 3.15.2.
Step 1: Write the Script
from pydantic import BaseModel, Field, ValidationError
class Product(BaseModel):
code: str = Field(pattern=r"^[A-Z]{3}-\d{4}$")
print(Product(code="BIK-1001"))
try:
Product(code="bike-1001")
except ValidationError as error:
print(error)

The pattern reads: ^ start, [A-Z]{3} three capital letters, - a dash, \d{4} four digits, $ end.
Step 2: Run It
uv run python product_code.py
code='BIK-1001'
1 validation error for Product
code
String should match pattern '^[A-Z]{3}-\d{4}$' [type=string_pattern_mismatch, input_value='bike-1001', input_type=str]
For further information visit https://errors.pydantic.dev/2.14/v/string_pattern_mismatch

BIK-1001 passed. bike-1001 failed because its letters are lower case, and the error quotes the pattern it had to match. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out ^ and $ | With pattern=r"[A-Z]{3}-\d{4}", the text "old BIK-1001 code" was accepted, because the pattern matched in the middle | Anchor the pattern at both ends |
Writing the pattern without r | In a plain string, "\d{4}" makes Python 3.15 warn: SyntaxWarning: "\d" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\d"? A raw string is also an option. | Write patterns as raw strings: r"^\d{4}$" |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
String should match pattern ... [type=string_pattern_mismatch ...] | The text has a different shape from the one the pattern describes | Compare the text with the pattern in the message, piece by piece |
| A value you expected to fail is accepted | The pattern isn't anchored with ^ and $ | Add them |
FAQ
Can I use pattern on a number field?
No: pattern is for strings. On an int field, creating an item with size=5 failed with TypeError: Unable to apply constraint 'pattern' to supplied value 5 for schema of type 'int'. For numbers, use the range checks gt, ge, lt and le.
Where can I test a pattern?
In Python's own re module. re.fullmatch checks the whole text, like an anchored pattern: re.fullmatch(r"[A-Z]{3}-\d{4}", "BIK-1001") printed <re.Match object; span=(0, 8), match='BIK-1001'>, and the same call on "old BIK-1001 code" printed None.
Conclusion
Field(pattern=r"^[A-Z]{3}-\d{4}$") accepted BIK-1001 and refused bike-1001 with string_pattern_mismatch. Anchor the pattern with ^ and $, and write it as a raw string.
Additional Resources
- Field —
patternand every other constraint. - Regular expression syntax — what
[A-Z],\dand{4}mean.
