简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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