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. Select a HostName with Regex in C# ?

Select a HostName with Regex in C# ?

Scheduled Pinned Locked Moved C#
csharpcomregextutorialquestion
11 Posts 5 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 Mohammad Dayyan

    Hi, There are some addresses as follows :

    http://rs320tl.rapidshare.com/files/119371167/sth.rar

    I'm gonna select rs320tl.rapidshare.com with Regex, but I'm not familiar with Regular Expressions. Would you please guide me ? Thanks. PS. rs320tl in the address is variable.

    T Offline
    T Offline
    tonyonlinux
    wrote on last edited by
    #2

    ^(ftp|http|file)://([^/]+)(/.*)?(/.*)

    so

    http :// www.codeproject.com /forum /xxx.html
    1 2 3 4

    so the reg expression would spit out $1 = http $2 = www.codeproject.com $3 = /forum $4 = /xxx.html hope this helps.

    M 2 Replies Last reply
    0
    • M Mohammad Dayyan

      Hi, There are some addresses as follows :

      http://rs320tl.rapidshare.com/files/119371167/sth.rar

      I'm gonna select rs320tl.rapidshare.com with Regex, but I'm not familiar with Regular Expressions. Would you please guide me ? Thanks. PS. rs320tl in the address is variable.

      A Offline
      A Offline
      AspDotNetDev
      wrote on last edited by
      #3

      Try this instead of a Regex:

      MessageBox.Show((new Uri("http://rs320tl.rapidshare.com/files/119371167/sth.rar")).Host);

      [Forum Guidelines]

      OriginalGriffO G M 3 Replies Last reply
      0
      • A AspDotNetDev

        Try this instead of a Regex:

        MessageBox.Show((new Uri("http://rs320tl.rapidshare.com/files/119371167/sth.rar")).Host);

        [Forum Guidelines]

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #4

        I didn't know that a Uri broke it down for you - thanks for that!:thumbsup:

        You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • A AspDotNetDev

          Try this instead of a Regex:

          MessageBox.Show((new Uri("http://rs320tl.rapidshare.com/files/119371167/sth.rar")).Host);

          [Forum Guidelines]

          G Offline
          G Offline
          Gaurav Dudeja India
          wrote on last edited by
          #5

          Under which namespace .host is come

          A 1 Reply Last reply
          0
          • G Gaurav Dudeja India

            Under which namespace .host is come

            A Offline
            A Offline
            AspDotNetDev
            wrote on last edited by
            #6

            I think you mean Uri. It's in the System namespace.

            [Forum Guidelines]

            1 Reply Last reply
            0
            • A AspDotNetDev

              Try this instead of a Regex:

              MessageBox.Show((new Uri("http://rs320tl.rapidshare.com/files/119371167/sth.rar")).Host);

              [Forum Guidelines]

              M Offline
              M Offline
              Mohammad Dayyan
              wrote on last edited by
              #7

              Thanks, but it throws an Exceptions on

              MessageBox.Show((new Uri("rs320tl.rapidshare.com")).Host);

              A 1 Reply Last reply
              0
              • T tonyonlinux

                ^(ftp|http|file)://([^/]+)(/.*)?(/.*)

                so

                http :// www.codeproject.com /forum /xxx.html
                1 2 3 4

                so the reg expression would spit out $1 = http $2 = www.codeproject.com $3 = /forum $4 = /xxx.html hope this helps.

                M Offline
                M Offline
                Mohammad Dayyan
                wrote on last edited by
                #8

                Thanks, How can I store $2 in a string?

                A 1 Reply Last reply
                0
                • M Mohammad Dayyan

                  Thanks, but it throws an Exceptions on

                  MessageBox.Show((new Uri("rs320tl.rapidshare.com")).Host);

                  A Offline
                  A Offline
                  AspDotNetDev
                  wrote on last edited by
                  #9

                  Try this:

                  MessageBox.Show((new UriBuilder("rs320tl.rapidshare.com")).Host);

                  [Forum Guidelines]

                  1 Reply Last reply
                  0
                  • M Mohammad Dayyan

                    Thanks, How can I store $2 in a string?

                    A Offline
                    A Offline
                    AspDotNetDev
                    wrote on last edited by
                    #10

                    string str = (new Regex("the pattern")).Match("the string").Groups[2].Value;

                    [Forum Guidelines]

                    1 Reply Last reply
                    0
                    • T tonyonlinux

                      ^(ftp|http|file)://([^/]+)(/.*)?(/.*)

                      so

                      http :// www.codeproject.com /forum /xxx.html
                      1 2 3 4

                      so the reg expression would spit out $1 = http $2 = www.codeproject.com $3 = /forum $4 = /xxx.html hope this helps.

                      M Offline
                      M Offline
                      Mohammad Dayyan
                      wrote on last edited by
                      #11

                      It doesn't work with www.codeproject.com

                      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