Using Regex in C# for ip:port format
-
Another thread came up with: @"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}[:][0-9]{1,5}$" But what if you want to constrain the octets to ONLY values from 0 to 255? And what if you really wanna go whole hog and constrain the port to 0 to 65535? I don't know regular expressions that well, so I am curious. Roink
Roink
-
Roink wrote:
But what if you want to constrain the octets to ONLY values from 0 to 255? And what if you really wanna go whole hog and constrain the port to 0 to 65535?
Then you head over to www.regexplib.com and you do a search[^]
-
As they say in Britain, Brilliant! Everything a growing RegEx coder needs! Thank you! Muchas gracias! Merci beaucoup!
Roink
-
Roink wrote:
As they say in Britain, Brilliant
I think they say that in other countries too, not just here in GB!
No, it's only there. :-D
-
That is true, too. You must also replace all the . with \. So, the regex is "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}$" Take in mind that also the above regex is not absolutely correct. For example, it matches also 999.999.999.999:3000.
-
Another thread came up with: @"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}[:][0-9]{1,5}$" But what if you want to constrain the octets to ONLY values from 0 to 255? And what if you really wanna go whole hog and constrain the port to 0 to 65535? I don't know regular expressions that well, so I am curious. Roink
Roink
Roink, Thanks for the solution. Following is working to check proper ip & port. I found this from regexlib.com. But still need to do varification for port as 0,00,000, etc or 0*.
@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]):(\d{1,4}|[0-5]\d\d\d\d|[0-5]\d\d\d\d|6[0-4]\d\d\d|65[0-4]\d\d|655[0-2]\d|6553[0-5])$"
Thanks & Regards, Aniket A. Salunkhemodified on Wednesday, December 3, 2008 4:47 AM