Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need regular expression to remove <a href="xx">Name</a> tags from a string [duplicate]

Tags:

c#

regex

asp.net

Need regular expression to remove the a tag from the following url <a href="http://example.com">Name</a> to output only the string "Name". I am using C#.net.

Any help is appreciated

like image 472
Cool Coder Avatar asked Apr 26 '11 15:04

Cool Coder


2 Answers

This will do a pretty good job:

str = Regex.Replace(str, @"<a\b[^>]+>([^<]*(?:(?!</a)<[^<]*)*)</a>", "$1");
like image 96
ridgerunner Avatar answered Oct 04 '22 23:10

ridgerunner


You should be looking at Html Agility Pack. RegEx works on almost all cases but it fails for some basics or broken Html. Since, the grammar of HTML is not regular, Html Agility pack still works perfectly fine in all cases.

If you are looking for just one time this particular case of anchor tag, any above RegEx would work for you, but Html Agility Pack is your long run, solid solution to strip off any Html tags.

Ref: Using C# regular expressions to remove HTML tags

like image 29
Priyank Avatar answered Oct 04 '22 22:10

Priyank