re = /(?:
(?<!\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/mix
str = '@username
@user_name
@username123
@alanbadoev @orkester okinea.t.me
@username123_, @username123
@username123, @username123_
@username123, username123.t.me
@name_with_underscore
@user$name
@user__name
@username_
@user name
@toolongusernameeeeeeeeeeeeeeeeeee
@123username
https://t.me/username
https://www.t.me/username
http://t.me/username
t.me/username
tg://resolve?domain=username
username.t.me
username.t.me
https://t.me/@username
http://t.me/user__name
username_.t.me
user__name.t.me
'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html