
我需要确保该字符串不包含西里尔字符。我这样检查:
from pydantic import basemodel, field
class mymodel(basemodel):
content_en: str = field(pattern=r"[^а-яА-Я]")
data = mymodel(content_en="has wrong content 'йцукен'")
print(data)
>>> content_en="has wrong content 'йцукен'"但是当我将包含西里尔字母的字符串传递到 content_en 字段时,不会引发错误。
预计:
pydantic_core._pydantic_core.validationerror: 1 validation error for mymodel ...
如何检查正确?
python 3.8
派丹蒂克2.5
解决方案(感谢@chepner):
class MyModel(BaseModel):
content_en: str = Field(pattern=r"^[^а-яА-ЯёЁ]*$")您的模式与包含至少一个非西里尔字符的任何字符串匹配,而不是仅由非西里尔字符组成的字符串。
>>> mymodel(content_en="has wrong content 'йцукен'")
mymodel(content_en="has wrong content 'йцукен'")
>>> mymodel(content_en="йцукен")
traceback (most recent call last):
file "<stdin>", line 1, in <module>
file "/users/chepner/py311/lib/python3.11/site-packages/pydantic/main.py", line 164, in __init__
__pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
pydantic_core._pydantic_core.validationerror: 1 validation error for mymodel
content_en
string should match pattern '[^а-яА-Я]' [type=string_pattern_mismatch, input_value='йцукен', input_type=str]
for further information visit https://errors.pydantic.dev/2.5/v/string_pattern_mismatch正确的模式是 ^[^а-яА-Я]*$:
>>> class MyModel(BaseModel):
... content_en: str = Field(pattern=r"^[^а-яА-Я]*$")
...
>>> MyModel(content_en="Has wrong content 'йцукен'")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/chepner/py311/lib/python3.11/site-packages/pydantic/main.py", line 164, in __init__
__pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
pydantic_core._pydantic_core.ValidationError: 1 validation error for MyModel
content_en
String should match pattern '^[^а-яА-Я]*$' [type=string_pattern_mismatch, input_value="Has wrong content 'йцукен'", input_type=str]
For further information visit https://errors.pydantic.dev/2.5/v/string_pattern_mismatch以上就是Pydantic 验证。检查字符串是否不包含某些字符的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号