【问题标题】:RegEx for the <li></li> tags<li></li> 标签的正则表达式
【发布时间】:2010-12-21 10:28:50
【问题描述】:

我正在开发 C# WinForm 应用程序。在那个应用程序中,我有这样的 sn-p:

<ul>
<li>abc
<li>bbc
<li>xyz
<li>pqr </li></li></li></li>     
</ul>

但是,我想得到像..这样的输出。

<ul>
<li>abc</li>
<li>bbc</li>
<li>xyz</li>
<li>pqr</li>
</ul>

有什么方法可以做到这一点吗?

谁能建议我解决这个问题的任何正则表达式?

谢谢。问候。

【问题讨论】:

  • 正则表达式如何解决这个问题?你在做什么?
  • 不明白为什么会有缺点。正常问题
  • 还有其他方法可以解决吗?我很好奇
  • 我会剥离所有标签,然后将 Environment.NewLine 拆分为一个数组。使用它来添加标签以使其正确。

标签: c# regex winforms visual-studio-2008


【解决方案1】:

不使用任何花哨的正则表达式很简单

试试下面,你可以实现自己的代码

   1. first Remove all </li>'s from the snippet
          line.replace("</li>","")
   2. Read each line starts with <li>
          if (line.startswith("<li">)
   3. and append the </li> at the end
          line+ ="</li>"
   4. combine all the line
           resString += line;

【讨论】:

    【解决方案2】:

    这适用于您的具体示例,但可能会在其他输入上中断(例如,如果 &lt;li&gt; 标记跨越换行符),因此如果它没有产生预期的结果,请编辑您的问题并提供更多详细信息。

    cleanString = Regex.Replace(subjectString, "(?:</li>)+", "", RegexOptions.IgnoreCase);
    resultString = Regex.Replace(cleanString, "<li>(.*)", "<li>$1</li>", RegexOptions.IgnoreCase);
    

    【讨论】:

    • 我运行这段代码,但有一些问题。最后一个 在 之后追加,但在 之前追加。
    • 它在您的示例中正常工作。但是,如果&lt;ul&gt;&lt;/li&gt; 在同一行,那么它将无法正常工作。请发布您正在使用的确切内容(编辑您的问题以这样做)。如果您提供不完整或不切实际的测试数据,您也会得到一个不完整的解决方案。
    • 我很好奇,如果你计时,它有多快?
    【解决方案3】:

    公共字符串 AddLiandOl(字符串 xhtml) {

                xhtml = xhtml.Replace("</li>", string.Empty);
                xhtml = xhtml.Replace("<li>", "</li><li>");
                xhtml = xhtml.Replace("</ol>", "</li></ol>");
                xhtml = xhtml.Replace("</ul>", "</li></ul>");
                Regex replaceul = new Regex("<ul>(.+?)</li>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                xhtml = replaceul.Replace(xhtml,"<ul>");
                Regex replaceol = new Regex("<ol>(.+?)</li>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                xhtml = replaceol.Replace(xhtml, "<ol>");
    
            return xhtml;
    
        }
    

    试试这个我已经测试过了。它有效...替换所有标签几乎不需要 30 秒..

    【讨论】:

      【解决方案4】:
      StringBuilder output = new StringBuilder("<ul>\n");
      foreach (i in Regex.Matches(snippet, "<li>\\w*"))
      {
          output.Append(i.Value).Append("</li>\n");
      }
      output.Append("\n</ul>");
      

      【讨论】:

        【解决方案5】:

        这不是解决您的问题的最漂亮的解决方案,但速度非常快。与直接字符串方法相比,正则表达式的速度较慢。

        我的字符串方法与 Tim Pietzcker 的两个 Regex.Replace 相比。 (对不起,蒂姆,我不得不选择一个人,你有赞成票:))

        这是 10,000 次重复。 numbers 是经过的刻度数:

        正则表达式替换: 平均:40.9659。最大:2273

        字符串替换: 平均:18.4566。最大:1478

        string strOrg = "<ul>\n" +
                        "<li>abc\n" +
                        "<li>bbc\n" +
                        "<li>xyz\n" +
                        "<li>pqr </li></li></li></li>\n" +
                        "</ul>";
        
        string strFinal = FixUnorderedList(strOrg);
        
        public static string FixUnorderedList(string str)
        {
            //remove what we're going to put back later
            //(these could be placed on the same line, one after the other)
            str = str.Replace("\n", string.Empty);
            str = str.Replace("</li>", string.Empty);
            str = str.Replace("<ul>", string.Empty);
            str = str.Replace("</ul>", string.Empty);
        
            //get each li element
            string[] astrLIs = str.Split(new string[] { "<li>" }, StringSplitOptions.RemoveEmptyEntries);
        
            //rebuild the list correctly
            string strFinal = "<ul>";
            foreach(string strLI in astrLIs)
                strFinal += string.Format("\n<li>{0}</li>", strLI.Trim());
        
            strFinal += "\n</ul>";
        
            return strFinal;
        }
        

        【讨论】:

          【解决方案6】:
                  string unorderlist = "<ul><li>ONE</li><li>TWO</li><li>THREE</li></ul>";
                  Regex regexul = new Regex("<ul>");
          
                  Match m = regexul.Match(unorderlist);
                  if (m.Success)
                  {
                      unorderlist = regexul.Replace(unorderlist, string.Empty);
                      Regex regex1 = new Regex("<li>");
                      unorderlist = regex1.Replace(unorderlist, ":");
                      Regex regex2 = new Regex("</li>");
                      unorderlist = regex2.Replace(unorderlist, "\n");
          
                      Regex regex3 = new Regex("</ul>");
                      unorderlist = regex3.Replace(unorderlist, "\n");
          
                      Console.WriteLine(unorderlist);
          
          
                  }
          

          【讨论】: