from typing import Union
from typing import Optional
import re


async def get_id(repr_str: Union[str, int], regex: str) -> Optional[int]:
    """
    Extracts an ID from either a string or integer.

    :param repr_str: ID string or integer
    :param regex: Regex matcher for ID
    :return: Integer containing required ID, or None if not found
    """
    if isinstance(repr_str, int):
        # If repr_str is just the ID itself
        return repr_str

    if isinstance(repr_str, str):
        # If repr_str is a string repr_stresentation of an integer
        id_str = repr_str.strip()
        if id_str.isnumeric():
            return int(id_str)

        # If repr contains integer matched by regex
        matches = re.search(regex, id_str)
        if matches:
            return int(matches.group(1))

    return None