C# Regex Port Validation 49152 to 65535
c#
Solution
Why using regular expression?
string input = "51204";
int portNumber;
if (int.TryParse(input, out portNumber)
&& portNumber >= 49152
&& portNumber <= 65535)
{
// Valid value...
}
Problem
After reading about ports and suggested ports I discovered that to comply with Internet Assigned Numbers Authority (IANA) recommendations for private ports you should only use ports from 49152 to 65535. As such I have attempted a regex pattern to check that the user has entered a port within that range. I have pieced this together with a great headache I had to read a lot of tutorials to get this far regex patters are really not human friendly. Basically I am looking for someone with more knowledge than me to verify that it is correct. My testing seems to indicate it but maybe I am missing something I just wanted to make sure. ``` string pattern = @"^(6553[0-5]|655[0-2]\d|65[0-4]\d\d|6[0-4]\d{3}|5\d{4}|49[2-9]\d\d|491[6-9]\d|4915[2-9])$"; ``` Regards!