revolutionkpi
revolutionkpi

Reputation: 2682

How to parse string with regular expression

How can I get List() from this text:

For the iPhone do the following:<ul><li>Go to AppStore</li><li>Search by him</li><li>Download</li></ul>

that should consist :Go to AppStore, Search by him, Download

Upvotes: 0

Views: 168

Answers (2)

Oded
Oded

Reputation: 499062

Load the string up into the HTML Agility Pack then select all li elements inner text.

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml("following:<ul><li>Go to AppStore</li><li>Search by him</li><li>Download</li></ul>");

var uls = doc.DocumentNode.Descendants("li").Select(d => d.InnerText);
foreach (var ul in uls)
{
    Console.WriteLine(ul);
}

Upvotes: 5

yamen
yamen

Reputation: 15618

Wrap in an XML root element and use LINQ to XML:

var xml = "For the iPhone do the following:<ul><li>Go to AppStore</li><li>Search by him</li><li>Download</li></ul>";
xml = "<r>"+xml+"</r>"; 
var ele = XElement.Parse(xml);  
var lists = ele.Descendants("li").Select(e => e.Value).ToList();

Returns in lists:

Go to AppStore 
Search by him 
Download

Upvotes: 2

Related Questions