http://www.perlmonks.org?node_id=338856


in reply to regex question

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.