Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Using Regex in C# for ip:port format

Using Regex in C# for ip:port format

Scheduled Pinned Locked Moved C#
csharpregexquestion
26 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Mirko1980

    This behavior is correct, because the regexp you wrote checks for matches contained in the input string. If you want to match the whole string only, you must add ^ at the begin and $ at the end of your regex. For Example: "^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}:\d{1,5}$"

    L Offline
    L Offline
    leppie
    wrote on last edited by
    #3

    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))

    J 1 Reply Last reply
    0
    • A Andy Rama

      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

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #4

      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))

      M 1 Reply Last reply
      0
      • A Andy Rama

        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

        N Offline
        N Offline
        Navneet Hegde
        wrote on last edited by
        #5

        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

        L 1 Reply Last reply
        0
        • N Navneet Hegde

          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

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #6

          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))

          N 1 Reply Last reply
          0
          • L leppie

            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))

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #7

            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)

            L 1 Reply Last reply
            0
            • J J4amieC

              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)

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #8

              J4amieC wrote:

              It wasn't that wrong.

              Yes, it was. Input 123a123b123c123, then it is completely wrong ;P

              xacc.ide - now with TabsToSpaces support
              IronScheme - 1.0 beta 1 - out now!
              ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

              1 Reply Last reply
              0
              • L leppie

                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))

                N Offline
                N Offline
                Navneet Hegde
                wrote on last edited by
                #9

                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

                N L 2 Replies Last reply
                0
                • N Navneet Hegde

                  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

                  N Offline
                  N Offline
                  Navneet Hegde
                  wrote on last edited by
                  #10

                  Hmmmm not working for 2221.1.1:3000 Sorry!

                  Develop2Program & Program2Develop

                  L 1 Reply Last reply
                  0
                  • N Navneet Hegde

                    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

                    L Offline
                    L Offline
                    leppie
                    wrote on last edited by
                    #11

                    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))

                    N 1 Reply Last reply
                    0
                    • N Navneet Hegde

                      Hmmmm not working for 2221.1.1:3000 Sorry!

                      Develop2Program & Program2Develop

                      L Offline
                      L Offline
                      leppie
                      wrote on last edited by
                      #12

                      Navneet Hegde wrote:

                      Hmmmm not working for 2221.1.1:3000

                      Well, that is not suppose to work...

                      xacc.ide - now with TabsToSpaces support
                      IronScheme - 1.0 beta 1 - out now!
                      ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

                      1 Reply Last reply
                      0
                      • L leppie

                        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))

                        M Offline
                        M Offline
                        Mirko1980
                        wrote on last edited by
                        #13

                        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.

                        A 1 Reply Last reply
                        0
                        • L leppie

                          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))

                          N Offline
                          N Offline
                          Navneet Hegde
                          wrote on last edited by
                          #14

                          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

                          L 1 Reply Last reply
                          0
                          • N Navneet Hegde

                            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

                            L Offline
                            L Offline
                            leppie
                            wrote on last edited by
                            #15

                            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))

                            N 1 Reply Last reply
                            0
                            • L leppie

                              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))

                              N Offline
                              N Offline
                              Navneet Hegde
                              wrote on last edited by
                              #16

                              Sure thx!

                              Develop2Program & Program2Develop

                              1 Reply Last reply
                              0
                              • A Andy Rama

                                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

                                P Offline
                                P Offline
                                PIEBALDconsult
                                wrote on last edited by
                                #17

                                I'd just try to open the port and let the framework figure it out.

                                L 1 Reply Last reply
                                0
                                • P PIEBALDconsult

                                  I'd just try to open the port and let the framework figure it out.

                                  L Offline
                                  L Offline
                                  Luc Pattyn
                                  wrote on last edited by
                                  #18

                                  PIEBALDconsult wrote:

                                  let the framework figure it out.

                                  and miss all the fun regexing IPv6?

                                  Luc Pattyn [Forum Guidelines] [My Articles]


                                  Fixturized forever. :confused:


                                  P 1 Reply Last reply
                                  0
                                  • A Andy Rama

                                    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

                                    R Offline
                                    R Offline
                                    Roink
                                    wrote on last edited by
                                    #19

                                    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

                                    J A 2 Replies Last reply
                                    0
                                    • L Luc Pattyn

                                      PIEBALDconsult wrote:

                                      let the framework figure it out.

                                      and miss all the fun regexing IPv6?

                                      Luc Pattyn [Forum Guidelines] [My Articles]


                                      Fixturized forever. :confused:


                                      P Offline
                                      P Offline
                                      PIEBALDconsult
                                      wrote on last edited by
                                      #20

                                      I'm more concerned about, "that which will come after IPv6". Let Microsoft do all the work, that's why I pay them. :-D

                                      1 Reply Last reply
                                      0
                                      • R Roink

                                        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

                                        J Offline
                                        J Offline
                                        J4amieC
                                        wrote on last edited by
                                        #21

                                        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[^]

                                        R 1 Reply Last reply
                                        0
                                        • J J4amieC

                                          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[^]

                                          R Offline
                                          R Offline
                                          Roink
                                          wrote on last edited by
                                          #22

                                          As they say in Britain, Brilliant! Everything a growing RegEx coder needs! Thank you! Muchas gracias! Merci beaucoup!

                                          Roink

                                          J 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups