Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 851
  • Last Modified:

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

0
Rad1
Asked:
Rad1
  • 2
1 Solution
 
informaniacCommented:
I'm sorry, but I din't get your question.
0
 
Rad1Author Commented:
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!
0
 
Rajar AhmedConsultantCommented:
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='http://www.boeing.com' target='_blank'>Hlink</a><a href='http://www.google.com' target='_blank'>Hlink</a> Testing how long it can go");
        Response.Write(s);
        
    }
    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

0
 
Rad1Author Commented:
Supper Excellent!!!!
Just perfect.

Thank you very kindly for your great work!

Rad1
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now