Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can't get Microsoft Regular Expression to work

Posted on 2006-07-19
5
Medium Priority
?
232 Views
Last Modified: 2010-07-27
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("^(http[s]?://)pear.nadd.tron.com/engrdocs/(?<port>:.+)$", RegexOptions.Compiled)
    Return r.Match(url).Result("${port}")
End Function
0
Comment
Question by:psmithphil
5 Comments
 
LVL 14

Expert Comment

by:jjardine
ID: 17140417
Why is it that you couldn't get the substing to work?     Would this work with the substring?

Function Extension(byVal url as string) as String
  If url.startsWith("https") Then
    Return url.Substring(31)
  ElseIf url.StartsWith("http:") Then
    Return url.Substring(30)
  End If
End Function

Check the starting index  they might be off by a few.
0
 
LVL 17

Accepted Solution

by:
ZeonFlash earned 1000 total points
ID: 17140526
You don't need the colon after <port>.

^(http[s]?://)pear.nadd.tron.com/engg/(?<port>.+)$

It's always the little things ;)
0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1000 total points
ID: 17140970
Hi psmithphil;

I am a person who likes using Regular Expressions when ever the situation calls for it. But in this situation you would be better using something like this

     Dim output As String = Url.Substring(url.IndexOf("engg/") + 5)

The reason is that the Regex will be much slower to get the same results.

Fernando
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 17141275
Hi psmithphil;

I did a little bench mark executing the call in the function 1000 times placing the code in the function in a for loop not calling the function 1000

This code which you posted with ZeonFlash's correction ran 5.148 seconds
Function Extension(ByVal Url As String) As String
    Dim r As New Regex("^(http[s]?://)pear.nadd.tron.com/engrdocs/(?<port>.+)$", RegexOptions.Compiled)
    Return r.Match(url).Result("${port}")
End Function

Using the code I posted where output is a string:

Ran 1000 times in a loop 1 millisecond.
     output = Url.Substring(url.IndexOf("engg/") + 5)

Now if you want to make the Regex run faster you can do the following.

    ' Make the Regex a class level variable
    Private r As New Regex("^(http[s]?://)pear.nadd.tron.com/engrdocs/(?<port>.+)$", RegexOptions.Compiled)

Function Extension(ByVal Url As String) As String
    Return r.Match(url).Result("${port}")
End Function

The above change will run the 1000 executions in 7 milliseconds. Now that time is just executing the code in the function 1000 times and not 1000 calls to the function.

The difference in time between your code and the modified code I posted above is due to the fact that I compile the Regex once and reuse it where you re-compile it every time you call the function.

Fernando
0
 

Author Comment

by:psmithphil
ID: 17142056
ZeonFlash, you had the answer to my problem as I just took the colon out and it worked.   I looked in the Help and online and never found how to use this.  I will use this in many applications - thank you!

FernandoSoto, I like the way you use the substring and I am actually going to use your way in this particular application.

You both helped immensely so I am splitting the points.  Thank you both!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question