Skip to content

RegularExpressionMatch

Qt Base Class: QRegularExpressionMatch

Signature: QRegularExpressionMatch(self) -> None QRegularExpressionMatch(self, match: PySide6.QtCore.QRegularExpressionMatch) -> None

Base classes

Name Children Inherits
QRegularExpressionMatch
PySide6.QtCore
QRegularExpressionMatch(self) -> None

⋔ Inheritance diagram

graph TD
  1473299846256["core.RegularExpressionMatch"]
  1473243789104["QtCore.QRegularExpressionMatch"]
  1473291690208["Shiboken.Object"]
  140713234304496["builtins.object"]
  1473243789104 --> 1473299846256
  1473291690208 --> 1473243789104
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: QRegularExpressionMatch

The results of a matching a QRegularExpression against a string.

Source code in prettyqt\core\regularexpressionmatch.py
class RegularExpressionMatch(core.QRegularExpressionMatch):
    """The results of a matching a QRegularExpression against a string."""

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.string = None
        self.pos = None
        self.endpos = None

    def __repr__(self):
        return get_repr(self)

    def __getitem__(self, item: int | str) -> str:
        if captured := self.captured(item):
            return captured
        else:
            raise KeyError(item)

    def __bool__(self):
        return self.isValid()

    def get_match_type(self) -> core.regularexpression.MatchTypeStr:
        return core.regularexpression.MATCH_TYPE.inverse[self.matchType()]

    def group(self, *groups: int | str) -> tuple[str, ...] | str:
        match len(groups):
            case 0:
                return self.captured(0)
            case 1:
                return self.captured(groups[0])
            case _:
                return tuple(self.captured(i) for i in groups)

    def groups(self, default=None) -> tuple:
        if self.lastindex is None:
            return ()
        return tuple(
            self.group(i) if i <= self.lastindex else default
            for i in range(self.re.captureCount())
        )

    def groupdict(self, default=None) -> dict[str, Any]:
        if self.lastindex is None:
            return {}
        groups = [
            self.group(i) if i <= self.lastindex else default
            for i in range(self.re.captureCount())
        ]
        names = self.re.namedCaptureGroups()
        return {names[i]: groups[i] for i in range(self.re.captureCount())}

    def start(self, group: int = 0) -> int:
        return self.capturedStart(group)

    def end(self, group: int = 0) -> int:
        return self.capturedEnd(group)

    def span(self, group: int = 0) -> tuple[int, int]:
        return (self.capturedStart(group), self.capturedEnd(group))

    @property
    def lastindex(self) -> int | None:
        idx = self.lastCapturedIndex()
        return None if idx == -1 else idx

    @property
    def lastgroup(self) -> str | None:
        if self.lastCapturedIndex() == -1:
            return None
        return self.re.namedCaptureGroups()[self.lastCapturedIndex()]

    @property
    def re(self) -> core.QRegularExpression:
        return self.regularExpression()

    @property
    def partial(self) -> bool:
        return self.hasPartialMatch()