Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

RegExp.Replace for anchor tags

Last post 01-15-2009, 3:57 AM by LeoTheLion. 2 replies.
Sort Posts: Previous Next
  •  01-10-2009, 10:17 AM 50012

    RegExp.Replace for anchor tags

    Hi Everyone,

    I have a slight problem with my Regular Expression. 

    I want to replace certain text in my anchor tag. For example if I have the following tag:

    <A HREF = '\\server\test\me.jpg'>Hello</A> 

    I want to replace test with live, such as:

    <A HREF = '\\server\live\me.jpg'>Hello</A>

    I'm using vb.net and so far I have come up with 

    LiteralDocumentation.Text = Regex.Replace(strText, "<a.*test*", "Live", RegexOptions.IgnoreCase) 

    The above statement gives me:

    Live\me.jpg'>Hello</A>

    So I'm missing the beginning part <A HREF = '\\server\. Any ideas what my regular expression pattern needs to be, so far I have <a.*test*

    Thanks

     

  •  01-10-2009, 11:35 AM 50016 in reply to 50012

    Re: RegExp.Replace for anchor tags

    Imports System
    Imports System.Text.RegularExpressions
    Module Module1
        Public Sub Main()
            Dim source As String = "<A HREF = '\\server\test\me.jpg' alt='test'>Hello</A>test<div id=""test"">test<A HREF = ""\\server\Test\test\me.jpg"">Hello</A>test"
            Dim r As Regex = New Regex("(<a\s[^>]*href\s*=\s*)(\S+)([^>]*>)", RegexOptions.IgnoreCase)
            Dim myEvaluator As MatchEvaluator = New MatchEvaluator(AddressOf RepFunc)
            Console.WriteLine("before:" & vbCrLf & source)
            source = r.Replace(source, myEvaluator)
            Console.WriteLine("after:" & vbCrLf & source)
        End Sub
        Public Function RepFunc(ByVal m As Match) As String
            Return m.Groups(1).Value & Regex.Replace(m.Groups(2).Value, "test", "Live", RegexOptions.IgnoreCase) & m.Groups(3).Value
        End Function
    End Module



    looking for a new regex book?
    Regular Expressions Cookbook
  •  01-15-2009, 3:57 AM 50136 in reply to 50016

    Re: RegExp.Replace for anchor tags

    Thanks,

     However, I found a way of doing this with one line:

    LiteralDocumentation.Text = Regex.Replace(strText, "(?<!href[^>]*)(" & Regex.Escape(Request.QueryString("Text")) & ")", "<span style=''background-color: #ffff00''><B>' & "$1" & "</B></span>", RegexOptions.IgnoreCase)

    "(?<!href[^>]*)(" & Regex.Escape(Request.QueryString("Text")) & ")", "<span style=''background-color: #ffff00''><B>' & "$1" & "</B></span>", RegexOptions.IgnoreCase)

    The only problem is that this only takes care of href but I also need to do this for src tags. Is there a way to do this such as:

    LiteralDocumentation.Text = Regex.Replace(strText, "(?<!href[^>]*)(" & Regex.Escape(Request.QueryString("Text")) & ")" + OR + "(?<!src[^>]*)(" & Regex.Escape(Request.QueryString("Text")) & ")", "<span style=''background-color: #ffff00''><B>' & "$1" & "</B></span>", RegexOptions.IgnoreCase)

    Any ideas? 

    Thanks

View as RSS news feed in XML