Search the web
Sign In
New User? Sign Up
codesnips · Code Snippets
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Message search is now enhanced, find messages faster. Take it for a spin.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
[C#] Stripping HTML tags from a given string   Message List  
Reply | Forward Message #35 of 48 |

The solution is quite simple:

1. Retrieve all the HTML tags using this pattern: <(.|\n)*?>

2. Replace them with an empty string and return the result

Here's a C# function that does this:

private string StripHTML(string htmlString)

{

//This pattern Matches everything found inside html tags;

//(.|\n) - > Look for any character or a new line

// *?  -> 0 or more occurences, and make a non-greedy search meaning

//That the match will stop at the first available '>' it sees, and not at the last one

//(if it stopped at the last one we could have overlooked

//nested HTML tags inside a bigger HTML tag..)

string pattern = @"<(.|\n)*?>";

 

return  Regex.Replace(htmlString,pattern,string.Empty);

}

Or with just one line of code:

string stripped = Regex.Replace(textBox1.Text,@"<(.|\n)*?>",string.Empty);

 



Sun May 18, 2003 3:46 pm

laghari78
Offline Offline
Send Email Send Email

Forward
Message #35 of 48 |
Expand Messages Author Sort by Date

The solution is quite simple: 1. Retrieve all the HTML tags using this pattern: <(.|\n)*?> 2. Replace them with an empty string and return the result Here's a...
Nauman Leghari
laghari78
Offline Send Email
May 18, 2003
3:47 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help