Click here to Skip to main content
13,900,419 members
Click here to Skip to main content
Add your own
alternative version

Stats

130.6K views
23 bookmarked
Posted 14 Dec 2010
Licenced CPOL

Remove all the HTML tags and display a plain text only inside (in case XML is not well formed)

, 18 Feb 2012
Rate this:
Please Sign up or sign in to vote.
Remove HTML and get a plain text from inside

Introduction



I was encouraged to write this Tip/Trick because of so many questions received for this issue.

Suppose you're having a bunch of HTML strings, but you just want to remove all the HTML tags and want a plain text.

You can use REGEX to come to the rescue.

The Regex I had developed before was more cumbersome, then Chris made a suggestion, so I will now go further with the regex suggested by Chris that is a "\<[^\>]*\>".
I have tested it for many cases. It detects all types of HTML tags, but there may be loopholes inside so if you find any tags which are not passing through this Regex, then kindly inform me about the same.

Regex Definition



  • Regex :\<[^\>]*\>

    • Literal >
    • Any character that NOT in this class:[\>], any number of repetations
    • Literal >



Program



string ss = "<b><i>The tag is about to be removed</i></b>";
        Regex regex = new Regex("\\<[^\\>]*\\>");
        Response.Write(String.Format("<b>Before:</b>{0}", ss)); // HTML Text
        Response.Write("<br/>");
        ss = regex.Replace(ss, String.Empty);
        Response.Write(String.Format("<b>After:</b>{0}", ss));// Plain Text as a OUTPUT


Program understanding



The above program just finds the matched Regex string and replaces the same with an empty string. Suppose you have an HTML String like "<li>Hiren</li>", then it will just output the string with simple "Hiren" as a PlainText.

Above sample Program OUTPUT



INPUT String : The tag is about to be removed
OUTPUT String : The tag is about to be removed

License

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

Share

About the Author

Hiren solanki
Software Developer
India India
He is a Smart IT devloper with Few years of Expeariance But having Great command on ASP.net,C#,SQL Query,SSRS,Crystal Reports

Apart from that He Loves multimedia work too, Master of Adobe photoshop, Illustrator, CSS , HTML and all things.

He is Currently working in Microsoft Dynamics CRM and Having Nice Expearince with CRM. CRM Rocks!!!

You may also be interested in...

Comments and Discussions

 
Questionbug Pin
Dileep M14-Mar-19 18:05
memberDileep M14-Mar-19 18:05 
QuestionHow to remove blank tags Pin
Khalas Dharmesh26-Jun-18 19:22
memberKhalas Dharmesh26-Jun-18 19:22 
QuestionI wish I had 4 hands - I'd give you 4 thumbs Up. Pin
237412-Dec-15 2:33
member237412-Dec-15 2:33 
GeneralMy vote of 5 Pin
Md Kamal Azhar19-Aug-15 1:10
memberMd Kamal Azhar19-Aug-15 1:10 
GeneralReason for my vote of 2 Doesn't work on tags that are over o... Pin
Corvus Corax24-Jan-11 18:25
memberCorvus Corax24-Jan-11 18:25 
GeneralReason for my vote of 1 Not secure or robust. See alternati... Pin
KevinAG17-Jan-11 14:19
memberKevinAG17-Jan-11 14:19 
GeneralThanks Pranay & suman. Pin
Hiren solanki21-Dec-10 21:43
memberHiren solanki21-Dec-10 21:43 
GeneralReason for my vote of 5 good one Pin
Pranay Rana21-Dec-10 20:58
professionalPranay Rana21-Dec-10 20:58 
GeneralReason for my vote of 5 Nice article. Pin
rp_suman20-Dec-10 6:53
memberrp_suman20-Dec-10 6:53 
GeneralThanks Eswa. Pin
Hiren solanki19-Dec-10 20:18
memberHiren solanki19-Dec-10 20:18 
GeneralReason for my vote of 5 Good Tip. Thanks Hiren. Pin
TweakBird18-Dec-10 1:49
memberTweakBird18-Dec-10 1:49 
GeneralThanks! Pin
Walt Fair, Jr.16-Dec-10 13:11
professionalWalt Fair, Jr.16-Dec-10 13:11 
GeneralThanks Chris, I updated my TIP with provided suggestion. Pin
Hiren solanki15-Dec-10 19:36
memberHiren solanki15-Dec-10 19:36 
General@Chris thanks for your wild suggestion, The regex you provid... Pin
Hiren solanki15-Dec-10 19:21
memberHiren solanki15-Dec-10 19:21 
Generala) This is fine for a single line, but will not do anything ... Pin
Chris Maunder15-Dec-10 19:08
adminChris Maunder15-Dec-10 19:08 
GeneralGood programmatic solution. 5 Pin
DrABELL17-Dec-10 6:23
professionalDrABELL17-Dec-10 6:23 
GeneralRe: Good programmatic solution. 5 Pin
Hiren solanki19-Dec-10 18:54
memberHiren solanki19-Dec-10 18:54 

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 | Cookies | Terms of Use | Mobile
Web01 | 2.8.190306.1 | Last Updated 18 Feb 2012
Article Copyright 2010 by Hiren solanki
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid