Monday, June 8, 2015

C# - Stripping all html from string fast!

Code example:


 /// <summary>  
     /// Compiled regular expression for performance.  
     /// </summary>  
     static Regex _htmlRegex = new Regex(@"(?></?\w+)(?>(?:[^>'""]+|'[^']*'|""[^""]*"")*)>", RegexOptions.Compiled);  
Alternavtive regex = static Regex _htmlRegex = new Regex(@"</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>", RegexOptions.Compiled);

     /// <summary>  
     /// Remove HTML from string with compiled Regex.  
     /// </summary>  
     public static string StripTagsRegexCompiled(string source)  
     {  
       return _htmlRegex.Replace(source, string.Empty);  
     }  

No comments:

Post a Comment