Regular Expressions 101

Community Library Entry

1

Regular Expression
Python

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$ # Ensure it ends with ".t.me" ) ) (?P<username> # Start capturing the username (?!\w*_{2,}) # Disallow double underscores in the username (?!\w*[^0-9a-z_.,\s]) # Ensure valid characters (letters, numbers, underscores, dots, commas, spaces) (?:[a-z][a-z0-9_]{3,31}) # Username (?<!_) # Disallow username ending with an underscore \b # Ensure it's a valid word boundary ) (?:\.t\.me)? # Optional ".t.me" subdomain
"
gmix

Description

Validates and obtains the Telegram user's username according to the rules for creating usernames

Submitted by okineadev - 2 months ago (Last modified 2 months ago)