Can't get Microsoft Regular Expression to work

I have urls that may start with "http" or "https" where I want to get the last parts of the url.

For example, in "http://pear.nadd.tron.com/engg/resources/dwglogs/sum/sumpdf/joesl.pdf", I want to get just "resources/dwglogs/sum/sumpdf/joesl.pdf". The first part is always one of two ways depending on whether the url has "http" or "https", and that is always followed by "://pear.nadd.tron.com/engg/". I want everything after that. I can't see a way to extract the last part of the url I want with SubString and/or IndexOf, so I tried using regular expressions. However, I can't get the example in the Microsoft help file to work (http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpconexampleextractingurlinformation.asp).

In the example below, I would want the function to return "resources/dwglogs/sum/sumpdf/joesl.pdf". Can you spot where I'm missing it Thanks!


Function Extension(ByVal Url As String) As String
Dim r As New Regex("^(httpSleep ://)pear.nadd.tron.com/engrdocs/( <port>:.+)$", RegexOptions.Compiled)
Return r.Match(url).Result("${port}")
End Function


Answer this question

Can't get Microsoft Regular Expression to work

  • David Joyce

    Thank you, OmegaMan, it works great! I searched on Help and searched around the Internet and never found any info on how to use this function or the "why" behind it. I don't know how you guys find stuff like this - I wish I could!

    Again, thank you!


    UPDATE 7/21/06:  I just went to use this and realized I missed something.  The code above will build the regEx, but I still need to get the value.  I get the value through adding the Return in a function:

    Function regexExtension(ByVal Url As String) As String
        Dim r As New Regex("^(httpSleep ://)pear.nadd.tron.com/engg/( <proto>.+$)", RegexOptions.Compiled)
        Return r.Match(Url).Result("${proto}")
    End Function

    It looks like "proto" is just a place holder for the value.  I tried "crap" and that worked fine too.

  • R.Tutus

    Try this one, it places it into groups for easier extraction. 

    '  Imports System.Text.RegularExpressions

    '  Regular expression built for Visual Basic on: Wed, Jul 19, 2006, 12:46:50 PM
    '  Using Expresso Version: 2.1.2150, http://www.ultrapico.com

    '  A description of the regular expression:

    '  [Protocol]: A named capture group. [\w+]
    '      Alphanumeric, one or more repetitions
    '  :\/\/
    '      :
    '      /
    '      /
    '  [Domain]: A named capture group. [[\w.]+\/ ]
    '      [\w.]+\/
    '          Any character in this class: [\w.], one or more repetitions
    '          /, zero or one repetitions
    '  [Rest]: A named capture group. [.*]
    '      Any character, any number of repetitions



    Public Dim MyRegex as Regex = New Regex( _
        "( <Protocol>\w+):\/\/( <Domain>[\w.]+\/ )( <Rest>.*)", _
        RegexOptions.IgnoreCase _
        Or RegexOptions.CultureInvariant _
        Or RegexOptions.IgnorePatternWhitespace _
        Or RegexOptions.Compiled _
        )

  • Can't get Microsoft Regular Expression to work