簡體   English   中英

正則表達式將多行混合大小寫字符串與C#中的空格匹配

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM