Parsing string and making list of required data
I am using **<style>..{data inside}..</style>**
which is there in following code.i have taken all the data between style tags in one string ,say string tempStyle and all operations are to be done on that string only.
I am looking for function which will take make a list of all "style" data. i.e. only style1,style2,style15,style20 into a list <>.
I don't want right,td,table tags in the List<>, i just want style data in list to be compared with another List. I am just looking for function to make List<> of style data which is there in between style tags.
please refer following code to understand the Question.
thanx in advance.
<html>
<head>
<style>
.right {
}
td{
}
table{
}
.style1{
开发者_如何转开发 }
.style2{
}
.style15{
}
.style20{
}
</style>
</head>
</html>
Regular expressions will do this nicely:
class Program
{
private const string PATTERN = @".style[\d]+{[^}]*}";
private const string STYLE_STRING = @" .right { } td{ } table{ } .style1{ } .style2{ } .style15{ } .style20{ }";
static void Main(string[] args)
{
var matches = Regex.Matches(STYLE_STRING, PATTERN);
var styleList = new List<string>();
for (int i = 0; i < matches.Count; i++)
{
styleList.Add(matches[i].ToString());
}
styleList.ForEach(Console.WriteLine);
Console.ReadLine();
}
}
精彩评论