using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(?(DEFINE)
(?<tagName>
[a-z][a-z\d]*+ (?=[>\s/])
)
(?<tagAttr>
(?>
[^>""']++
| "" [^""]*+ ""
| ' [^']*+ '
)*+
)
)
< (li|/ul) /?+ (?=[>\s/]) ((?&tagAttr)) >
((?:
[^<]++
| < /?+ (?!(?:li|ul)(?=[>\s/])) (?&tagName) (?&tagAttr) > #any tag except <li> and <ul>
| < (?! /?+ (?&tagName) ) #not tag
)*)
(?= < (?:li|/ul) (?=[>\s/]) (?&tagAttr) ) #check for nested <li> and <ul>";
string substitution = @"<$3$4>$5</LI>\r";
string input = @"<ul>
<li fhgfhfg ghgfhfghhfgh= ghghg=""..."" a = 'ghghg'>dfdsfsd
<ul>
<li>111</li>
<li/>2222
<li>mmmmm</li>
</ul>
<li>dfdsfdsfsdf <b class=""...""><br/>fgdfgd<i></b><!-- dfdf--><li>
<li>
<li>dsfsdfsdfdsf
</ul>
";
RegexOptions options = RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.CultureInvariant;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx