Skip to content

RegularExpressionValidator

Base classes

Name Children Inherits
ValidatorMixin
prettyqt.gui.validator
QRegularExpressionValidator
PySide6.QtGui
QRegularExpressionValidator(self, /, parent: PySide6.QtCore.QObject | None \= None, *, regularExpression: PySide6.QtCore.QRegularExpression | None \= None) -> None

⋔ Inheritance diagram

graph TD
  3064719559["gui.RegularExpressionValidator"]
  1804432572["gui.ValidatorMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  222390816["QtGui.QRegularExpressionValidator"]
  49520861["QtGui.QValidator"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  1804432572 --> 3064719559
  3227531497 --> 1804432572
  2751680898 --> 3227531497
  2751680898 --> 1804432572
  222390816 --> 3064719559
  49520861 --> 222390816
  3984060476 --> 49520861
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: ValidatorMixin, QRegularExpressionValidator

Used to check a string against a regular expression.

Source code in prettyqt/gui/regularexpressionvalidator.py
class RegularExpressionValidator(gui.ValidatorMixin, gui.QRegularExpressionValidator):
    """Used to check a string against a regular expression."""

    ID = "regular_expression"

    def __init__(self, *args, **kwargs):
        # allow passing strings as well as re.Pattern to the ctor
        match args, kwargs:
            case (str() as pat, *rest), _:
                super().__init__(core.QRegularExpression(pat), *rest, **kwargs)
            case _, {"regular_expression": str() as reg_str, **rest}:
                pat = core.QRegularExpression(reg_str)
                super().__init__(*args, regular_expression=pat, **rest)
            case (re.Pattern() as pat,), _:
                super().__init__(core.RegularExpression(pat))
            case _, _:
                super().__init__(*args, **kwargs)

    def __repr__(self):
        return get_repr(self, self.regularExpression())

    def __getstate__(self):
        return dict(pattern=core.RegularExpression(self.regularExpression()))

    def __setstate__(self, state):
        self.setRegularExpression(state["pattern"])

    def __reduce__(self):
        return type(self), (), self.__getstate__()

    def __eq__(self, other: object):
        return (
            self.regularExpression() == other.regularExpression()
            if isinstance(other, type(self))
            else False
        )

    def set_regex(
        self,
        regex: datatypes.PatternAndStringType,
        flags=None,
    ):
        self.setRegularExpression(datatypes.to_regular_expression(regex))

    def get_regex(self) -> str:
        val = self.regularExpression()
        return val.pattern()

:fa-brands-github: Show source on GitHub