Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
1.25/5 (4 votes)
string orderlist = "<ol><li>ONE</li><li>TWO</li><li>THREE</li></ol>";

In this string, how to find howmany no.of <li> 's ?

Actually I want an output like this...?

1. ONE
2. TWO
3. THREE
Posted
Updated 6-Jan-11 0:19am
v2
Comments
Sandeep Mewara 6-Jan-11 8:42am    
Multiple reposts.

You need to parse the string using those nodes, just like we do while deserializing an xml string.
 
Share this answer
 
Since this isn't quite a repost - just very, very, very nearly...

You have been given a regex which returns the text content of the HTML. You just need to expand it to extract the text from the "li" section into separate matches, and then process them.
Since you are only interested in the "li" sections, It may be worth your while changing the regex to:
string inputText = "<ol><li>ONE</li><li>TWO</li><li>THREE</li></ol>";
Regex regex = new Regex("(?<=\\<li\\>)[^\\<]*(?=\\</li\\>)",
                        RegexOptions.IgnoreCase
                        | RegexOptions.CultureInvariant
                        | RegexOptions.IgnorePatternWhitespace
                        | RegexOptions.Compiled);
MatchCollection ms = regex.Matches(inputText);
string outText = "Matches:\n";
foreach (Match m in ms)
    {
    outText += m.Groups[0].Value + "\n";
    }
I would strongly suggest you go to www.ultrapico.com[^] and D/L a copy of Expresso - it helps design, explain and test regular expressions. It's free, it generates C# or VB code for you, and it works brilliantly - I wish I'd written it!
 
Share this answer
 
For finding no of li's

C#
string li = "<ol><li>ONE</li><li>TWO</li><li>THREE</li></ol>";
        Regex regex = new Regex(@"li");
        int count = regex.Matches(li).Count;


For other stuff see OrginalGriff's answer.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900