Skip to content

parsing

ParseNumberFromBrackets

Extracts the size from a string in the form of prefix[22].

Source code in pyiceberg/utils/parsing.py
class ParseNumberFromBrackets:
    """Extracts the size from a string in the form of prefix[22]."""

    regex: Pattern  # type: ignore
    prefix: str

    def __init__(self, prefix: str):
        self.prefix = prefix
        self.regex = re.compile(rf"{prefix}\[(\d+)\]")

    def match(self, str_repr: str) -> int:
        matches = self.regex.search(str_repr)
        if matches:
            return int(matches.group(1))
        raise ValidationError(f"Could not match {str_repr}, expected format {self.prefix}[22]")