views:

35

answers:

1

hi,

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>
+1  A: 

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();
    }
}
Nicholas Cloud
Looks nice buddy but i ws looking fo dynamic...style values may differ
Sangram
I'm not sure I understand what you're looking for. This will grab all the styles and their content.
Nicholas Cloud
looking for all possible occurrences of style i can't make it hardcore for style1,styl2,style15..regex may help but i am not in regex..skip right,td,table tags s
Sangram
every time for different files style data will change..so i want it to work for all possible conditions. so it w+il check style dataS are there and will make list<> of style dataS present in <style> </style> tag. I really appreciate your help.
Sangram
The regular expression will match any style declaration like this: style### { //any content here }, so as long as your pages conform to that pattern, you should be good. The regex pattern will only return those particular styles. If, for some reason, you cannot use regular expressions, you would need to manually do string manipulation, which will be a serious pain.
Nicholas Cloud
still ur regex idea will help me a lot...thnx a lot.. i will match style and add in list like u have done..
Sangram