Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Regular Expression to append a querysting to URLs in a HTML page

Options
  • 03-07-2006 2:50pm
    #1
    Closed Accounts Posts: 47


    I'm trying to write a script which will append a query string parameter/value to all urls in a html page.

    I have the following code almost working
    Dim _regex As New System.Text.RegularExpressions.Regex("href\s*=\s*\""(?<url>[^\""]*)\""", RegexOptions.IgnoreCase)
            Dim mc As Text.RegularExpressions.MatchCollection = _regex.Matches(StringBuffer.ToString, 0)
            For Each m As Text.RegularExpressions.Match In mc
                            StringBuffer = Regex.Replace(StringBuffer, m.Value, m.Value & "&k=" & k)
            Next
    

    However, is there a better way to do this without iterating through the MatchCollection?

    Can I do this in one line by doing something like
    _Regex.replace(StringBuffer, <?url>& "&k=" & k)
    
    ?

    I expect I'll get to the bottom of this, its just that I assume its something thats been done many times before. I just can't seem to find any help on google.


Comments

  • Closed Accounts Posts: 47 Lucifer 2


    this is what i've gone for in the end.
    Dim _regex As New System.Text.RegularExpressions.Regex("href\s*=\s*\""(?<url>[^\""]*)\""", RegexOptions.IgnoreCase)
            StringBuffer = _regex.Replace(StringBuffer.ToString, AddressOf UrlAppender)
    
    Private Function UrlAppender(ByVal M As Text.RegularExpressions.Match) As String
            UrlAppender = M.Groups("url").Value
            If UrlAppender.Contains("?") Then
                UrlAppender = UrlAppender & "&k=" & _smartkey
            Else
                UrlAppender = UrlAppender & "?k=" & _smartkey
            End If
            Return M.Value.Replace(M.Groups("url").Value, UrlAppender)
        End Function
    


Advertisement