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.
  • 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
                      • R Roink

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

                        Roink

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

                        Roink wrote:

                        As they say in Britain, Brilliant

                        I think they say that in other countries too, not just here in GB!

                        P 1 Reply Last reply
                        0
                        • J J4amieC

                          Roink wrote:

                          As they say in Britain, Brilliant

                          I think they say that in other countries too, not just here in GB!

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

                          No, it's only there. :-D

                          1 Reply Last reply
                          0
                          • M Mirko1980

                            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 Offline
                            A Offline
                            Andy Rama
                            wrote on last edited by
                            #25

                            Mirko1980 wrote:

                            "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}$"

                            Thanks. It's working to check proper format of string. Still I am working to check proper formt of string with correct ip address & port. Thanks & Regards, Aniket A. Salunkhe

                            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

                              A Offline
                              A Offline
                              Andy Rama
                              wrote on last edited by
                              #26

                              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. Salunkhe

                              modified on Wednesday, December 3, 2008 4:47 AM

                              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