Regular expressions to check whether the IP address and port number are legal, the code is as follows:
Regular expressions to detect IP address
p>
public static bool CheckAddress(string s)
{
bool isLegal = false;
Regex regex = new Regex(@”^((2[0-4]\d|25[ 0-5]|[1]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[1]?\d\d?)$” );
Match match = regex.Match(s);//Can test other ip
//Match match = regex.Match(“192.168.1.666”);//Can test other ip
if (match.Success)
{
isLegal = true;
Console.WriteLine($”match”);
Console.WriteLine($”match.Value:{match.Value}”);
}
else
{
isLegal = false;
Console.WriteLine($”Does not match”);
}
return isLegal;
}
Regex regex detection port number
public static bool CheckPort(string s)
{
bool isLegal = false;
Regex regex = new Regex(@”^[1-9]$|(^[1-9][0-9]$)|(^[1-9][0-9][0-9]$)|(^ [1-9][0-9][0-9][0-9]$)|(^[1-6][0-5][0-5][0-3][0-5] $)”);//CheckPort
//Match match = regex.Match(“8087”);//You can test other ip ports 0-65535
Match match = regex.Match(s);/ /Can test other ip
if (match.Success)
{
isLegal = true;
Cons ole.WriteLine($”match”);
Console.WriteLine($”match.Value:{match.Value}”);
}
else
{
isLegal = false;
Console.WriteLine($”Does not match”);
}
return isLegal;
}
Regular expression to detect IP address and port Is the number legal
static bool CheckAddressPort(string s) {bool isLegal = false; Regex regex = new Regex(@”^((2[0-4]\d|25[0-5]| [1]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[1]?\d\d?)\:([1-9 ]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]| [1-6][0-5][0-5][0-3][0-5])$”);//CheckAddressPort Match match = regex.Match(s); //Match match = regex. Match(“192.168.1.2:33333″); //You can test other ip and port if (match.Success) {isLegal = true; Console.WriteLine($”match”); Console.WriteLine($”match.Value: {match.Value}”);} else {isLegal = false; Console.WriteLine($”Does not match”);} return isLegal; }