Using Regex in C# for ip:port format
-
Hi all, I am using C#.Net 2008. I am using following code to varify proper format (udp ip:port i.e "225.1.1.1:3000") of input string. But it is not working properly. System.Text.RegularExpressions.Regex regStr = new System.Text.RegularExpressions.Regex(@"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}:\d{1,5}"); bool b; b = regStr.IsMatch("225.1.1.1:3000"); //return true b = regStr.IsMatch("2225.1.1.1:3000"); //return true. should return false Anybody knows proper regular expression for ip:port format? Can anyone give me good links for using Regex , regular expression in C#. Thanks in advance. Regards, Aniket A. Salunkhe
Try This : System.Text.RegularExpressions.Regex regStr = new System.Text.RegularExpressions.Regex(@"^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}:[0-9]{1,5}$"); Thanks!
Develop2Program & Program2Develop
-
Mirko1980 wrote:
For Example: "^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}:\d{1,5}$"
Wrong, try again.
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x)) -
Try This : System.Text.RegularExpressions.Regex regStr = new System.Text.RegularExpressions.Regex(@"^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}:[0-9]{1,5}$"); Thanks!
Develop2Program & Program2Develop
-
It wasn't that wrong. I have to be honest I missed the unescaped . and thought of the other half of the problem (^,$ to match beginning and end)
-
Wrong! Same mistake as Mirko1980. Try 123a123a123a123, your regex will match that too!
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))But this work's fine. MessageBox.Show(System.Text.RegularExpressions.Regex.IsMatch("123a123a123a123", @"^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}:[0-9]{1,5}$", System.Text.RegularExpressions.RegexOptions.ExplicitCapture).ToString()); Thanks!
Develop2Program & Program2Develop
-
But this work's fine. MessageBox.Show(System.Text.RegularExpressions.Regex.IsMatch("123a123a123a123", @"^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}:[0-9]{1,5}$", System.Text.RegularExpressions.RegexOptions.ExplicitCapture).ToString()); Thanks!
Develop2Program & Program2Develop
Hmmmm not working for 2221.1.1:3000 Sorry!
Develop2Program & Program2Develop
-
But this work's fine. MessageBox.Show(System.Text.RegularExpressions.Regex.IsMatch("123a123a123a123", @"^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}:[0-9]{1,5}$", System.Text.RegularExpressions.RegexOptions.ExplicitCapture).ToString()); Thanks!
Develop2Program & Program2Develop
-
Hmmmm not working for 2221.1.1:3000 Sorry!
Develop2Program & Program2Develop
-
You need to escape the '.' else it will match anything.
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x)) -
Sorry make that 123a123a123a123:1
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))This should work @"^[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[:][0-9]{1,5}$" Thanks!
Develop2Program & Program2Develop
-
This should work @"^[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[:][0-9]{1,5}$" Thanks!
Develop2Program & Program2Develop
Navneet Hegde wrote:
This should work
Rather use \. than [.] . Some regex implementations might see [.] as .
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x)) -
Navneet Hegde wrote:
This should work
Rather use \. than [.] . Some regex implementations might see [.] as .
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))Sure thx!
Develop2Program & Program2Develop
-
Hi all, I am using C#.Net 2008. I am using following code to varify proper format (udp ip:port i.e "225.1.1.1:3000") of input string. But it is not working properly. System.Text.RegularExpressions.Regex regStr = new System.Text.RegularExpressions.Regex(@"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}:\d{1,5}"); bool b; b = regStr.IsMatch("225.1.1.1:3000"); //return true b = regStr.IsMatch("2225.1.1.1:3000"); //return true. should return false Anybody knows proper regular expression for ip:port format? Can anyone give me good links for using Regex , regular expression in C#. Thanks in advance. Regards, Aniket A. Salunkhe
I'd just try to open the port and let the framework figure it out.
-
I'd just try to open the port and let the framework figure it out.
PIEBALDconsult wrote:
let the framework figure it out.
and miss all the fun regexing IPv6?
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
Hi all, I am using C#.Net 2008. I am using following code to varify proper format (udp ip:port i.e "225.1.1.1:3000") of input string. But it is not working properly. System.Text.RegularExpressions.Regex regStr = new System.Text.RegularExpressions.Regex(@"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}:\d{1,5}"); bool b; b = regStr.IsMatch("225.1.1.1:3000"); //return true b = regStr.IsMatch("2225.1.1.1:3000"); //return true. should return false Anybody knows proper regular expression for ip:port format? Can anyone give me good links for using Regex , regular expression in C#. Thanks in advance. Regards, Aniket A. Salunkhe
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
-
PIEBALDconsult wrote:
let the framework figure it out.
and miss all the fun regexing IPv6?
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
I'm more concerned about, "that which will come after IPv6". Let Microsoft do all the work, that's why I pay them. :-D
-
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