Wednesday 20 January 2016

Regular Expression (Regex) to extract Inner Text from HTML HyperLink control (Anchor tags)

Extract inner text from Anchor tag
Description:-

In this example we explain that how to get or extract the inner text from the HTML hyper link or asp.net link control using Regular expression(Regex) in asp.net using C#. Or how to get or extract inner text from html Anchor tag</a> using Regex expression.

In this example I have one textbox and one submit button in which textbox contain text including hyperlink and when I click on submit button then it extract only text (remove anchor tag or hyperlink) and display in one label control.

So how to extract or fetch only text from the Anchor tag or from Hyperlink using Regular Expression (Regex) in asp.net using C#.
extractlink1.aspx:-

<%@ Page Language="C#" ValidateRequest="false" AutoEventWireup="true" CodeFile="extractlink1.aspx.cs"
    Inherits="WebApplication1_extractlink1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Regular Expression (Regex) to extract Inner Text from HTML HyperLink (Anchor
        tags)</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="txtlinkaddress" runat="server" TextMode="MultiLine" Height="150px"
        Width="500px" Text="<a href = 'http://www.Facebook.com'>Facebook</a> is a Social Media. <a href = 'http://www.gmail.com'>Gmail</a> is a Email tool."></asp:TextBox><br />
    <asp:Button ID="btnremovelink" runat="server" Text="Remove Hyperlinks" OnClick="btnremovelink_Click" />
    <br />
    <asp:Label ID="lbltextmsg" runat="server" Text=""></asp:Label>
    </form>
</body>
</html>

 extractlink1.aspx.cs:-

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 WebApplication1_extractlink1 : System.Web.UI.Page
{

    protected void btnremovelink_Click(object sender, EventArgs e)
    {
        lbltextmsg.Text = Regex.Replace(txtlinkaddress.Text, "</?(a|A).*?>", "");
    }

}


0 comments:

Post a Comment