简体   繁体   English

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

[英]RegEx for the <li></li> tags

i am working on the C# WinForm application. 我正在研究C#WinForm应用程序。 In that application, i have snippet like this: 在那个应用程序中,我有这样的代码片段:

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

but, i want to get output like.. 但是,我希望得到像......的输出

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

Is there any method using which this thing can be done? 有什么方法可以用来做这件事吗?

Can anybody suggest me any RegEx for this problem? 任何人都可以建议我这个问题的任何RegEx吗?

Thanks. 谢谢。 Regards. 问候。

Its simple without using any fancy regex 它简单而不使用任何花哨的正则表达式

Try below, you can implement your own code 尝试下面,您可以实现自己的代码

   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;

This works on your specific example, but may well break on other input (for example, if <li> tags were to span linebreaks), so if it's not producing the desired results, please edit your question with more details. 这适用于您的具体示例,但可能会在其他输入中中断(例如,如果<li>标记要跨越换行符),因此如果它没有产生所需的结果,请编辑您的问题并提供更多详细信息。

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

public string AddLiandOl(string xhtml) { 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;

    }

Try this i have tested it. 试试这个我已经测试过了。 it works...It hardly takes 30 seconds to replace all tags.. 它的工作原理......更换所有标签几乎不需要30秒..

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

This isn't the prettiest solution to your problem, but it's crazy fast. 这对你的问题来说不是最漂亮的解决方案,但它很快就疯了。 Regex's are slow compared to straight string methods. 与直字符串方法相比,正则表达式很慢。

My string method compared to Tim Pietzcker's two Regex.Replace's. 我的字符串方法与Tim Pietzcker的两个Regex.Replace相比。 (Sorry Tim, I had to pick on someone, and you have the upvote :) ) (对不起蒂姆,我不得不挑选一个人,你有upvote :))

this is with 10,000 reps. 这是10,000名代表。 numbers are number of elapsed ticks: 数字是经过的刻度数:

regex replace: avg: 40.9659. 正则表达式替换:平均值:40.9659。 max: 2273 最大:2273

string replace: avg: 18.4566. string replace:avg:18.4566。 max: 1478 最大: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;
}
        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);


        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM