[英]Regex matching a multiline, mixed case string with whitespace in C#
我试图保证CMS中的字段仅包含无序列表。 例如,
<ul>
<li>
This is our first bullet point
</li>
</ul>
我正在使用以下内容来匹配它:
String pattern = "^<ul>(<li>.*</li>)+</ul>$";
Regex rgx = new Regex(@pattern,
RegexOptions.IgnorePatternWhitespace
| RegexOptions.Multiline
| RegexOptions.IgnoreCase);
if(rgx.IsMatch(controlValidationValue)) { ... }
当html全部在一行上时,此方法有效,但是当我出现换行符或空格时会失败-这可能会发生,因为我们的CMS使用富文本插件创建了html。
我试过使用按位与(而不是OR),并与RegexOptions.SingleLine
一起RegexOptions.SingleLine
但无法RegexOptions.SingleLine
。
任何/所有帮助表示赞赏!
通常,我会使用HtmlAgilityPack
解析HTML而不是正则表达式。
string html = @"<ul>
<li>
This is our first bullet point
</li>
</ul>";
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html.Trim()); // Trim to remove leading or trailing spaces if that's possible
bool valid = doc.DocumentNode.ChildNodes.Count == 1
&& doc.DocumentNode.ChildNodes[0].Name == "ul";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.