CSS Parser Wanted

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rasmus

    CSS Parser Wanted

    I need a way to extract css class names from a css file. Therefore I’m
    looking for a CSS Parser, preferably freeware and written in C#.

    Anybody know of such one?



  • John Timney \( MVP \)

    #2
    Re: CSS Parser Wanted

    use a regular expression



    --
    Regards

    John Timney
    Microsoft MVP

    "Rasmus" <Rasmus@newsgro up.nospam> wrote in message
    news:ACF00980-C0A4-4860-97C1-922182B5DE46@mi crosoft.com...[color=blue]
    >I need a way to extract css class names from a css file. Therefore I'm
    > looking for a CSS Parser, preferably freeware and written in C#.
    >
    > Anybody know of such one?
    >
    >
    >[/color]


    Comment

    • Rasmus

      #3
      Re: CSS Parser Wanted

      Thanks for the quick reply.

      I'm not too good at using regex in c#. Could you provide a snippet of sample
      code?

      Comment

      • John Timney \( MVP \)

        #4
        Re: CSS Parser Wanted

        from google



        --
        Regards

        John Timney
        Microsoft MVP

        "Rasmus" <Rasmus@newsgro up.nospam> wrote in message
        news:B37FFE55-5F8B-4848-9AA3-AE4993307121@mi crosoft.com...[color=blue]
        > Thanks for the quick reply.
        >
        > I'm not too good at using regex in c#. Could you provide a snippet of
        > sample
        > code?
        >[/color]


        Comment

        • Rasmus

          #5
          Re: CSS Parser Wanted

          Can't seem to get it to work...



          static void Main(string[] args){
          string css = @"
          ..class1{
          color:pink;
          }

          ..class2{
          color:blue;
          }";

          string patt =
          @"\.[-]?[_a-zA-Z][_a-zA-Z0-9-]*|[^\0-\177]*\\[0-9a-f]{1,6}(\r\n[
          \n\r\t\f])?|\\[^\n\r\f0-9a-f]*";
          string[] arr = Regex.Split(css , patt);
          }

          Comment

          • John Timney \( MVP \)

            #6
            Re: CSS Parser Wanted

            According to the author the code should be as show below
            I gave out a bit of duff information in my last post. Thanks Rasmus!The code should have been:string css = @".class1{color:pink;}.class2{color:blue;}";string patt = @".[-]?[_a-zA-Z][_a-zA-Z0-9-]|[^\0-\177]\[0-9a-f]{1,6}(\r\n[ \n\r\t\f])?|\[^\n\r\f0-9a-f]*";MatchCollection arr = Regex.Matches(css, patt);}The "arr" Collection will contain all the class names found in the input string. arr.Count will be the number of matches, and it can be indexed like so: arr[0].Value etc. etc.Sorry about that :)

            --
            Regards

            John Timney
            Microsoft MVP


            The code should have been:

            string css = @"
            ..class1{
            color:pink;
            }
            ..class2{
            color:blue;
            }";
            string patt =
            @"\.[-]?[_a-zA-Z][_a-zA-Z0-9-]*|[^\0-\177]*\\[0-9a-f]{1,6}(\r\n[
            \n\r\t\f])?|\\[^\n\r\f0-9a-f]*";
            MatchCollection arr = Regex.Matches(c ss, patt);
            }




            "Rasmus" <Rasmus@newsgro up.nospam> wrote in message
            news:30EF0ABA-BE45-4409-939A-2B1074904170@mi crosoft.com...[color=blue]
            > Can't seem to get it to work...
            >
            >
            >
            > static void Main(string[] args){
            > string css = @"
            > .class1{
            > color:pink;
            > }
            >
            > .class2{
            > color:blue;
            > }";
            >
            > string patt =
            > @"\.[-]?[_a-zA-Z][_a-zA-Z0-9-]*|[^\0-\177]*\\[0-9a-f]{1,6}(\r\n[
            > \n\r\t\f])?|\\[^\n\r\f0-9a-f]*";
            > string[] arr = Regex.Split(css , patt);
            > }[/color]


            Comment

            • Kinlan
              New Member
              • Feb 2006
              • 2

              #7
              Yeah, I made some simple mistakes in the original example but it is sorted.

              if you want all the Matches then you need to use Regex.Matches.

              I haven't done too much work on it since, but I will be trying to make more Regex's for parsing CSS.

              www.kinlan.co.u k

              Comment

              • Kinlan
                New Member
                • Feb 2006
                • 2

                #8
                CSS Classname Regex

                I have update the regex.

                I've updated my C# regex for extracting CSS class names to correctly handle URLs in CSS properties like url(someimage.png). The previous version incorrectly matched file extensions. The improved regex uses a negative lookbehind assertion (?

                Comment

                Working...