Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

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

  • 03-07-2006 02: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