import typing from marshmallow.validate import Regexp import regex class UnicodeRegexp(Regexp): """Customized validator to support extended Unicode constructions with regex lib. Supports constructions like '\p{L}' (any letter of any language), that the standard python re module doesn't. .. note:: Uses `regex.match`, which searches for a match at the beginning of a string. :param pattern: The regular expression string to use. Can also be a compiled regular expression pattern. :param flags: The regexp flags to use, for example re.IGNORECASE. Ignored if ``pattern`` is not a string. :param error: Error message to raise in case of a validation error. Can be interpolated with `{input}` and `{pattern}`. """ def __init__( self, pattern: str | bytes | typing.Pattern, flags: int = 0, *, error: str | None = None, ): self.regex = ( regex.compile(pattern, flags) if isinstance(pattern, (str, bytes)) else pattern ) self.error = error or self.default_message