def match_regex(self, regex: Pattern, required: bool = False,
                    meaning: str = "") -> str:
        """Parse input based on a regular expression .

        Args:
            regex: Compiled regular expression object.
            required: Should the exception be raised on unexpected input?
            meaning: Meaning of `regex` (for use in error messages).

        Raises:
            UnexpectedInput: If no syntactically correct keyword is found.
        """
        mo = regex.match(self.input, self.offset)
        if mo:
            self.offset = mo.end()
            return mo.group()
        if required:
            raise UnexpectedInput(self, meaning)