RegEx用于<li> </li>标签

时间:2010-12-21 10:28:50

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

我正在使用C#WinForm应用程序。在那个应用程序中,我有这样的代码片段:

<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>

有什么方法可以用来做这件事吗?

有人可以建议我解决这个问题吗?

感谢。问候。

6 个答案:

答案 0 :(得分:2)

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

请尝试以下操作,您可以实现自己的代码

   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;

答案 1 :(得分:2)

这适用于您的具体示例,但可能会在其他输入中中断(例如,如果<li>标记跨越换行符),那么如果它没有产生所需的结果,请编辑您的问题并提供更多详细信息

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

答案 2 :(得分:1)

public string AddLiandOl(string 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秒..

答案 3 :(得分:0)

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>");

答案 4 :(得分:0)

这对你的问题来说不是最漂亮的解决方案,但它很快就疯了。与直字符串方法相比,正则表达式很慢。

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

这是10,000名代表。数字是经过的刻度数:

正则表达式替换: 平均值: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;
}

答案 5 :(得分:0)

        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);


        }
相关问题