Have you thought about using split

# for example: use strict; use warnings; my $CSS = ""; my $filename = shift (@ARGV) || die ("No file in \@ARGV!"); open ("FILE", "< $filename") or die ("Cannot open $filename $!"); $CSS .= $_ while (<FILE>); close ("FILE") or die ("Cannot close $filename $!"); # CSS is of the form: # H1 { /* CSS */ } # where H1 is the HTML element # what's in {} is what to do # and /* */ is comments # so get rid of comments $CSS =~ s[/\*.*?\*/][]sg; # now get what's in between the {}s my @parts = split /\}/, $CSS; while (@parts) { my $part = shift (@parts); # now we should have CSS of the form # H1 { directives my ($HTML_element, $CSS) = split /\{/, $part, 2; # note that the HTML element we're working on is in # $HTML_element # now get the CSS directives my @directives = split ';', $CSS; # now each CSS directive i.e. # font-family: Verdana, Arial, Geneva # has been brokon off foreach my $directive (@directives) { my @parts = split ':', $directive; # $parts[0] is font-family my @fonts = split ',', $parts[0]; # @fonts is all the different font families } }

You'll note I did what I did because CSS states that there's a : after a property, i.e. font-family and that there are commas between different options. In any event, why not go to CPAN and pick up a ready made CSS parser? What I did was just a quick hack, and although it should give you some direction on what you can do, it almost certainly won't work nearly as well as a CPAN module -- of which there are many.


Want to support the EFF and FSF by buying cool stuff? Click here.

In reply to Re: regex question by Vautrin
in thread regex question by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.