If you delete the HTML tags, it may be difficult to read (such as A, IMG tags), it is best to delete part of the reservation part.
In regular expressions, it is very easy to understand that some strings are included, but it is really a puzzle to know how to tell if you don't include some strings (strings, not characters, some, not some).
< (?! ((/?\s?li) | (/?\s?ul) | (/?\s?a) | (/?\s?img) | (/?\S?BR) | (/?\s?span) | (/?\s?b))) [^>]+>
The regular is to determine that the HTML tag does not contain li/ul/a/img/br/span/b, as far as the above requirements, is to remove the HTML tag except the one listed here, which I groped for a long time to get out.
(?! EXP) match the location followed by the EXP
/?\s? I initially tried to write it to the front <, but the test failed.
Here is a simple function, the tag to keep the string up, generate a regular expression, and then delete the unwanted tag ...
private static string removespecifyhtml (String ctx) {string[] Holdtags = {"A", "IMG", "BR", "strong", "B", "span"};//to be insured Left tag//< (?!) ((/?\s?li) | (/?\s?ul) | (/?\s?a) | (/?\s?img) | (/?\S?BR) | (/?\s?span) | (/?\s?b))) [^>]+> string regstr = string. Format @ < (?! (/?\s? {0}))) [^>]+> ", String. Join (@ ") | (/?\s? ", Holdtags)); Regex reg = new Regex (Regstr, regexoptions.compiled | Regexoptions.multiline | Regexoptions.ignorecase); Return Reg. Replace (CTX, "");}
Correction:
Above the regular, if the Li is retained, the actual operation will find that link is also retained, the retention of a will be addr also to retain, the solution is to add \b assertion.
< (?! ((/?\s?li\b) | (/?\s?ul) | (/?\s?a\b) | (/?\s?img\b) | (/?\s?br\b) | (/?\s?span\b) | (/?\s?b\b))) [^>]+> private static string removespecifyhtml (String ctx) {string[] Holdtags = {"A", "IMG", "BR", "strong", "B", "Span", "li"};//reserved Tag//< (?! ((/?\s?li\b) | (/?\s?ul\b) | (/?\s?a\b) | (/?\s?img\b) | (/?\s?br\b) | (/?\s?span\b) | (/?\s?b\b))) [^>]+> string regstr = string. Format @ < (?! (/?\s? {0}))) [^>]+> ", String. Join (@ "\b) | (/?\s? ", Holdtags)); Regex reg = new Regex (Regstr, regexoptions.compiled | Regexoptions.multiline | Regexoptions.ignorecase); Return Reg. Replace (CTX, "");}