开发者

RegEx for the <li></li> tags

i am working on the C# WinForm application. In that application, i have snippet like this:

<ul>
<li>abc
<li>bbc
<li>xyz
<li>pqr </li></li>开发者_如何学Python;</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?

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.

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


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


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. (Sorry Tim, I had to pick on someone, and you have the upvote :) )

this is with 10,000 reps. numbers are number of elapsed ticks:

regex replace: avg: 40.9659. max: 2273

string replace: avg: 18.4566. max: 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);


        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜