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*__) # Disallow double underscores anywhere in the username
(?!\w*_{2,}) # Disallow underscores at the start or end of username
(?:[a-z][a-z0-9_]{3,31}) # Username
(?<!_) # Disallow usernames ending with an underscore
\.t\.me$