Results 1 to 8 of 8

Thread: Regex and URLs [Resolved]

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Resolved Regex and URLs [Resolved]

    Using Regex (or perhaps any other way), how can I strip the homepage of a URL like this one :

    http://www.vbforums.com/newthread.ph...ead/forumid=25

    so I get this result :

    http://www.vbforums.com


    Thanks ,
    Last edited by Pirate; Nov 12th, 2004 at 09:42 PM.

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Found this and modified it a little...
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, _
    2.                               ByVal e As System.EventArgs) Handles Button1.Click
    3.         Dim s As String = "http://www.vbforums.com/newthread.php?s=&action/newthread/forumid=25"
    4.         Dim sp As String = "^(http:\/\/)?([^\/]+)"
    5.         Dim ms As MatchCollection
    6.  
    7.         ms = Regex.Matches(s, sp)
    8.  
    9.         MsgBox(ms(0).Value)
    10.  
    11.     End Sub
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Be sure to import System.Text.RegularExpressions

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Resolved

    Works solid . Thanks !

  5. #5
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Another way, not as cool as regex, but certainly less cryptic
    VB Code:
    1. Dim s As String = "http://www.vbforums.com/newthread.php?s=&action/newthread/forumid=25"
    2.         Dim chop As Integer
    3.         chop = s.IndexOf("/", 7)
    4.         Dim s2 As String = s.Substring(0, chop)
    5.         Debug.WriteLine(s2)

    Of course that's assuming some things, but you know what I mean.

  6. #6

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Mike Hildner
    Another way, not as cool as regex, but certainly less cryptic
    VB Code:
    1. Dim s As String = "http://www.vbforums.com/newthread.php?s=&action/newthread/forumid=25"
    2.         Dim chop As Integer
    3.         chop = s.IndexOf("/", 7)
    4.         Dim s2 As String = s.Substring(0, chop)
    5.         Debug.WriteLine(s2)

    Of course that's assuming some things, but you know what I mean.
    It won't work if a url starts with (https) . Nice try

  7. #7
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    That may be true, but allow me to quote myself
    Of course that's assuming some things, but you know what I mean.
    That example hard coded the seventh position. One could easily determine the position of a double slash, if that worked for you. It's not my point to try do out do regex, but as your title implied, I'm just suggesting another way.

    Not that I recommend one over the other.

  8. #8

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Yah I guess , it's another way . It's cool and if I add some stuff it would as accurate as regex (lots of work) . Thanks though . This gives some insights to other things .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width