r"
(?:
(?<!\S)@ # Match "@" only if it's not preceded by a non-whitespace character
| # OR
(?:
# Telegram link types reference: https://core.telegram.org/api/links
#
(?:https?://)? # Optional HTTP or HTTPS protocol
(?:www\.)? # www subdomain
(?:t\.me|telegram\.(?:me|dog)) # Match "t.me", "telegram.me", or "telegram.dog" domains
/ # Ensure a forward slash after the domain
| # OR
tg://resolve\?domain= # Match Telegram deep link schema (tg://resolve?domain=)
)
| # OR
(?=\b # Positive lookahead: Ensure a valid subdomain before username
(?!\w*__)