Skip to content

Instantly share code, notes, and snippets.

@creadone
Created August 4, 2023 00:18
Show Gist options
  • Save creadone/2eac6243806a6721724b4a0c20e82f43 to your computer and use it in GitHub Desktop.
Save creadone/2eac6243806a6721724b4a0c20e82f43 to your computer and use it in GitHub Desktop.
Telegram username validation and extraction
test_str = %w[
userNAME_bla12
@userNAME_bla12
t.me/userNAME_bla12
tg://resolv?domain=userNAME_bla12
https://telegram.me/userNAME_bla12/2312313
https://userNAME_bla12.t.me
]
regexes = {
simple: /^([a-zA-Z0-9_]{5,32})$/,
mention: /^@([a-zA-Z0-9_]{5,32})$/,
web: /^https?:\/\/(?:t|telegram)\.me\/([a-zA-Z0-9_]{5,32})(?:\/\d+)$/,
internal: /^tg:\/\/resolv\?domain=([a-zA-Z0-9_]{5,32})$/,
subdomain: /^https?:\/\/([a-zA-Z0-9_]{5,32})\.t\.me$/
}
test_str.each do |str|
regexes.each do |name, rule|
if str.match?(rule)
username = str.match(rule).captures.first
puts "#{name} => #{username}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment