using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(?:
(?<!\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";
string input = @"@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
";
RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
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 C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx