【问题标题】:How do I parse a string with an unordered list into a generic list of strings?如何将具有无序列表的字符串解析为通用字符串列表?
【发布时间】:2019-08-14 19:08:46
【问题描述】:

我有一个无序列表作为字符串存储在我的 SQL 表中。我需要稍后在 Excel 文件中显示它,但我无法正确格式化它。

我尝试过使用正则表达式,我想我已经接近了,但我错过了一些东西。

这是我的示例输入字符串

<ul>
    <li>Line 1</li>
    <li>Line 2</li>
    <li>Line 3</li>
    <li>Line 4
        <ul style="list-style-type:circle">
            <li>Line 4-1</li>
            <li>Line 4-2
                <ul style="list-style-type:square">
                    <li>Line 4-2-1</li>
                    <li>Line 4-2-2</li>
                    <li>Line 4-2-3</li>
                </ul>
            </li>
            <li>Line 4-3</li>
        </ul>
    </li>
    <li>Line 5</li>
    <li>Line 6</li>
    <li>Line 7</li>
</ul>

这是我到目前为止所做的。

            var dt = new DataTable();
            dt.Columns.Add();

string inputValue; //unordered list from above

            Regex rgxLI = new Regex(@"<li>(.*?)</li>");
            Regex rgxCircle = new Regex(@"<ul style=\""list-style-type:circle\"">(.*?)</ul>");
            Regex rgxSquare = new Regex(@"<ul style=\""list-style-type:square\"">(.*?)</ul>");

            MatchCollection mcLI = rgxLI.Matches(inputValue);
            for (var i = 0; i < mcLI.Count; i++)
            {
                DataRow dr = dt.NewRow();
                //string instructionLine = mc[i].Value;
                if (mcLI[i].Value.Contains("<ul style=\"list-style-type:circle\">"))
                {
                    MatchCollection mcCircle = rgxCircle.Matches(mcLI[i].Value);
                    for (var j = 0; j < mcCircle.Count; j++)
                    {
                        if (mcLI[j].Value.Contains("<ul style=\"list-style-type:square\">"))
                        {
                            MatchCollection mcSquare= rgxSquare.Matches(mcLI[j].Value);
                            dr[0] = System.Net.WebUtility.HtmlDecode("&#9642;" + mcSquare[j].ToString().Replace("<li>", "").Replace("</li>", ""));
                        }
                        else
                        {
                            dr[0] = System.Net.WebUtility.HtmlDecode("&#8226;" + mcCircle[j].ToString().Replace("<li>", "").Replace("</li>", ""));
                        }
                    }

                }
                else
                {

                    dr[0] = System.Net.WebUtility.HtmlDecode(mcLI[i].Value.Replace("<li>", "").Replace("</li>", ""));

                }
                dt.Rows.Add(dr);

            }

不确定我是否过于复杂或只是错过了一些步骤。我能够解析大部分字符串,但在 4-1 之后我错过了要点。

【问题讨论】:

    标签: c# asp.net-mvc html-lists generic-list


    【解决方案1】:

    你可以试试这个:

    List<string> list = new List<string>();
    
    list = (Regex.Split(YOURSTRING, "\r\n")).ToList<string>();
    

    这应该只是按每一行分割。 它将拆分为一个数组,这就是我使用 .ToList() 的原因。

    "\r\n" 用于用正则表达式查找换行符

    【讨论】:

      猜你喜欢
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 2018-02-07
      相关资源
      最近更新 更多