Locked Regular expression for replacing anchor tags

  • Tuesday, August 14, 2012 3:28 PM
     
     

    Hi,

    I need some more help, Colud you please

    provide a code snippet, in which anchor tags are replaced with href values and the inner text of anchor tag

      in the below mentioned format i.e.

    <a href="www.msdn.com">msdn link</a> hi test text <test>dsgfdsh<test><a name="test" href="www.msdn.com">msdn2 link</a>

    above one should be replaced to 

    ~%msdn link^www.msdn.com%~ hi test text <test>dsgfdsh<test> ~%msdn2 link^www.msdn.com%~

    Thank you in advance.

All Replies

  • Tuesday, August 14, 2012 5:27 PM
     
      Has Code

    Since I have no idea what you HTML looks like I am completely shooting in the dark. Try to use the HTML Agility pack to grab the specific nodes you want for easier extrapolation of nodes. Also, your example is obviously in some other nodes that you are not showing. Your text is not in anything meaningful:

    <name>
        <a href="www.msdn.com">msdn link</a>
        hi test text <test>dsgfdsh<test>                               <--see what I mean
        <a name="test" href="www.msdn.com">msdn2 link</a>
    </name>

    Any who, I just did a basic replace:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                String additional = "~%msdn link^www.msdn.com%~";
                String anchor = @"<a href=""www.msdn.com"">msdn link</a> hi test text <test>dsgfdsh<test><a name=""test"" href=""www.msdn.com"">msdn2 link</a>";
    
                anchor = anchor.Replace("hi", additional + " hi");
                anchor = anchor.Replace("<test><a", "<test>" + additional + "<a");
            }
        }
    }
    
    //~%msdn link^www.msdn.com%~ hi test text <test>dsgfdsh<test> ~%msdn2 link^www.msdn.com%~

    If I had more information and more HTML, it would be helpful. As I am not sure if you are just giving me values that you already pulled out.

  • Wednesday, August 15, 2012 9:04 AM
     
     

    Hi,

    For example there is an html text which consists of anchor tag as shown below

    <b>test</b><a href="www.msdn.com">click here</a><li>test list</li>

    I want to replace only anchor tag in below format,

    <b>test</b>~#click here^www.msdn.com#~<li>test list</li>


  • Wednesday, August 15, 2012 9:56 AM
    Moderator
     
     Answered Has Code

    Hi Gopi,

    Welcome to the MSDN Forum.

    Please try this code:

    anchor = Regex.Replace(anchor,@"<a.*?href=""(?<link>.*?)"">(?<content>.*?)</a>",@"~#${content}^${link}#~")

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Wednesday, August 15, 2012 10:30 AM
     
     

    Thank you so much mike, Its working.