Convert links to text using Regex

Posted on 2012-09-22
Last Modified: 2012-09-28

The below function finds the first link in the string and assign it to the rest of the links in a string.  How can I make it replace Hlink with the correct link text?

Thank you!!!

This is an example of the input string:  This is a Project <a href="" target=_blank>Hlink</a>
<a href="" target=_blank>Hlink</a> Testing how long it can go.

    public string LinkToString(string aStr)
            //System.Text.RegularExpressions.Regex re = new Regex(@"((https?|ftp|file)\://|www.)[A-Za-z0-9\.\-]+(/[A-Za-z0-9\?\&\=;\+!'\(\)\*\-\._~%]*)*/g", RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex re = new Regex("((http://|https|file|www\\.)([A-Z0-9.-:]{1,})\\.[0-9A-Z?;~&#=\\-_\\./]{2,})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.MatchCollection mc = re.Matches(theStr);
            foreach (Match m in mc)
                theStr = theStr.Replace("Hlink", m.Value);
            return Regex.Replace(aStr, "<(.|\n)*?>", string.Empty);

Open in new window

Question by:Rad1
    LVL 20

    Expert Comment

    I'm sorry, but I din't get your question.

    Author Comment

    Pass the string I provided and you will see what I mean.   I want the function to replace each hlink with the correct hyperlink.

    Also The string supposed to have a clickable hlink instead of the link it self.  Sorry!

    What the function does now is replace the hlink with first link it finds.

    Thank you!
    LVL 18

    Accepted Solution

    try this,
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text.RegularExpressions;
    public partial class cs_hLinkReplace : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            string s = LinkToString("This is a Project <a href='' target='_blank'>Hlink</a><a href='' target='_blank'>Hlink</a> Testing how long it can go");
        public string LinkToString(string aStr)
            Regex re = new Regex("((http://|https|file|www\\.)([A-Z0-9.-:]{1,})\\.[0-9A-Z?;~&#=\\-_\\./]{2,})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.MatchCollection mc = re.Matches(aStr);
            foreach (Match m in mc)
                aStr = ReplaceFirstOccurrance(aStr, "Hlink", m.Value);
            return Regex.Replace(aStr, "<(.|\n)*?>", string.Empty);
        public static string ReplaceFirstOccurrance(string original, string oldValue, string newValue)
            if (String.IsNullOrEmpty(original))
                return String.Empty;
            if (String.IsNullOrEmpty(oldValue))
                return original;
            if (String.IsNullOrEmpty(newValue))
                newValue = String.Empty;
            int loc = original.IndexOf(oldValue);
            return original.Remove(loc, oldValue.Length).Insert(loc, newValue);

    Open in new window


    Author Closing Comment

    Supper Excellent!!!!
    Just perfect.

    Thank you very kindly for your great work!


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now