Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Yeah I know its a totally n00b question, so I appreciate someone pointing me in the right direction.

I am attempting to write a pattern matching script to set variable.

Pattern is one of a URL. I am trying to basically say:
"If file is in this folder tree 3 folders deep set Dim to x"

So here is what I have
VB
Dim strBanner
If Request.ServerVariables("URL") like "*/vendors/[*]/[*]/
index.aspx*"  Then
    strBanner = Whatever


End If


I was looking for a way to catch anything in between "/ /"

Thanks for any help!
Posted
Updated 14-Mar-11 3:37am
v2
Comments
Manfred Rudolf Bihy 14-Mar-11 9:38am    
Edit: Fixed pre tags.

1 solution

You are looking for a regular expression. This is what Expresso came up with:
VB
'  Imports System.Text.RegularExpressions

'  Regular expression built for Visual Basic on: Mon, Mar 14, 2011, 01:42:46 PM
'  Using Expresso Version: 3.0.3634, http://www.ultrapico.com
'
'  A description of the regular expression:
'
'  /vendors/
'      /vendors/
'  Any character that is NOT in this class: [/], any number of repetitions
'  /
'  Any character that is NOT in this class: [/], any number of repetitions
'  /index.aspx
'      /index
'      Any character
'      aspx
'
'

Public Dim regex As Regex = New Regex( _
      "/vendors/[^/]*/[^/]*/index.aspx", _
    RegexOptions.Singleline _
    Or RegexOptions.CultureInvariant _
    Or RegexOptions.IgnorePatternWhitespace _
    Or RegexOptions.Compiled _
    )


' This is the replacement string
Public Dim regexReplace As String = _
      "<Hello>"


'' Replace the matched text in the InputText using the replacement pattern
' Dim result As String = regex.Replace(InputText,regexReplace)

'' Split the InputText wherever the regex matches
' Dim results As String() = regex.Split(InputText)

'' Capture the first Match, if any, in the InputText
' Dim m As Match= regex.Match(InputText)

'' Capture all Matches in the InputText
' Dim ms As MatchCollection = regex.Matches(InputText)

'' Test to see if there is a match in the InputText
' Dim IsMatch As Boolean = regex.IsMatch(InputText)

'' Get the names of all the named and numbered capture groups
' Dim GroupNames As String() = regex.GetGroupNames()

'' Get the numbers of all the named and numbered capture groups
' Dim GroupNumbers As Integer() = regex.GetGroupNumbers()


If you are going to use them in the future, get a copy: it's free, and it helps explain, design, and check Regexes. I wish I'd written it!
 
Share this answer
 
Comments
Manfred Rudolf Bihy 14-Mar-11 10:32am    
[Moved from OP's solution]
Thanks :) , im going to give them a try!
Manfred Rudolf Bihy 14-Mar-11 10:34am    
Thanks for that Expresso tip! 5+
OriginalGriff 14-Mar-11 10:36am    
Welcome! I wouldn't be without it nowadays!
Manfred Rudolf Bihy 14-Mar-11 10:38am    
Installed it already :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900