import re


def get_url(id, hashtag):
    """Return URL to a hashtag or a user."""

    if hashtag:
        # Pattern for hashtags.
        url = re.match(
            "^http[s]?://www.instagram.com/explore/tags/[a-zA-Z0-9_]+", id
        )
        name = re.match("^[a-zA-Z0-9_]+$", id)
    else:
        # Patterns for usernames.
        url = re.match(
            r"^http[s]?://www.instagram.com/[a-zA-Z0-9_\.]{2,30}[/]?$", id
        )
        name = re.match(r"^[a-zA-Z0-9_\.]{2,30}$", id)

    # Return full username or hashtag url.
    if url:
        return url.group()
    elif name and hashtag:
        return "https://www.instagram.com/explore/tags/" + name.group()
    elif name and not hashtag:
        return "https://www.instagram.com/" + name.group()
    # Shut down program if some unexpected error occurres.
    else:
        raise SystemExit(
            "Some error occurred, couldn't match username or hashtag."
        )