import re


def FormatText(Text):
    """
    Adds HTML links to any Instagram accounts, hashtags, or URLs in a text string. Also replaces newlines with "<br>".
    """
    Text = str(Text)
    Text = re.sub(r"https?:\/\/\S+", lambda match: '<a href=\"{0}\">{0}</a>'.format(match.group()), Text) # Add HTML links to web addresses.
    Text = re.sub(r"@\S+", lambda match: '<a href=\"https://www.instagram.com/{0}\">@{0}</a>'.format(match.group()[1:]), Text) # Add HTML links to user accounts.
    Text = re.sub(r"#\S+", lambda match: '<a href=\"https://www.instagram.com/explore/tags/{0}">#{0}</a>'.format(match.group()[1:]), Text) # Add HTML links to hashtags.
    Text = re.sub("\\n", "<br>", Text) # Replace "\n" with "<br>".
    return Text