Click here to Skip to main content
13,550,352 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 8 Feb 2011
Licenced CPOL

Regular Expression to extract inner text from anchor tags

, 8 Feb 2011
Rate this:
Please Sign up or sign in to vote.
Regular Expression to extract inner text from anchor tags

Several days ago, someone at the forum has asked how to extract the text from a hyperlink and preserve other HTML tags. It sounded interesting, I did some research but could not find a direct solution. So, I decided to put together a simple regular expression to execute the task.

Regular Expression: (<[a|A][^>]*>|</[a|A]>)


<[a|A][^>]*> -- Remove <a ...>

</[a|A]> -- Remove </a> tag

Example 1:

string str1 = "<a href=\"\" class=\"someclass\">Mastering Regular Expressions</a> 
-- <A href=\"\">CNN</a> <div><a href=\"\"></a></div>";

str1 = System.Text.RegularExpressions.Regex.Replace(str1, "(<[a|A][^>]*>|)", "");

Result: Mastering Regular Expressions -- CNN <div> </div>

Example 2:
string str2 = "<div><a href=\"\" class=\"someclass\">ysatech</a></div>";

str2 = System.Text.RegularExpressions.Regex.Replace(str2, "(<[a|A][^>]*>|)", "");

Result: <div>ysatech</div>

Test this regular expression here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Bryian Tan
Software Developer (Senior)
United States United States
I have over 10 years of experience working with Microsoft technologies. I have earned my Microsoft Certified Technology Specialist (MCTS) certification. I'm a highly motivated self-starter with an aptitude for learning new skills quickly.

You may also be interested in...

Comments and Discussions

QuestionHow to use Regular Expression to extract anchor href link Pin
Member 1187233629-Jul-15 8:03
memberMember 1187233629-Jul-15 8:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180515.1 | Last Updated 9 Feb 2011
Article Copyright 2011 by Bryian Tan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid