Link to home
Start Free TrialLog in
Avatar of Rad1
Rad1

asked on

Convert links to text using Regex

Hello,

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="http://www.boeing.com" target=_blank>Hlink</a>
<a href="http://www.google.com" 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

Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

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

ASKER

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!
ASKER CERTIFIED SOLUTION
Avatar of Rajar Ahmed
Rajar Ahmed
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rad1

ASKER

Supper Excellent!!!!
Just perfect.

Thank you very kindly for your great work!

Rad1