Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am writing a script to parse css files. I need a regex that will seperate lines similar to this:
font-family: Verdana, Arial, Geneva, san-serif; font-size: .69em; color: #666666
Into something like this:
Verdana, Arial, Geneva, san-serif
The numbers of names and values is dynamic. Some css selectors have 1 name value pair, some have half a dozen. I can write a regex that grabs one name value pair but I am stumbling over writing one that will match each pair in the string. Thanks in advance for any help!

Replies are listed 'Best First'.
Re: regex question
by graff (Chancellor) on Mar 23, 2004 at 02:15 UTC
    It's simple: split that line wherever there is a colon or semicolon followed by space -- just to be safe, we'll accept one or more whitespace characters of any kind:
    my $fontline = "font-family: Verdana, Arial, Geneva, san-serif; font-s +ize: .69em; color: #666666"; my @components = split /[:;]\s+/, $fontline; print join "\n", "", @components, ""; print "\n or, as a hash:\n"; %components = @components; print "$_ => $components{$_}\n" for ( keys %components ); __OUTPUT__ font-family Verdana, Arial, Geneva, san-serif font-size .69em color #666666 or, as a hash: font-family => Verdana, Arial, Geneva, san-serif font-size => .69em color => #666666
Re: regex question
by Vautrin (Hermit) on Mar 23, 2004 at 00:56 UTC

    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.
Re: regex question
by leriksen (Curate) on Mar 23, 2004 at 02:53 UTC
    Same as graff but focussing solely on the hash solution - I also didn't worry about spaces after the split determinants

    #!/usr/bin/perl -w use strict; my %data = split(/[:;]/, 'font-family: Verdana, Arial, Geneva, san-ser +if; font-size: .69em; color: #666666'); use Data::Dumper; print STDERR Dumper(\%data);
    $VAR1 = { 'font-family' => ' Verdana, Arial, Geneva, san-serif', ' color' => ' #666666', ' font-size' => ' .69em' };

    use warnings;use strict;use brain;

Re: regex question
by Anonymous Monk on Mar 23, 2004 at 15:42 UTC
    Thanks for the help all :) The solution I ended using was writing an adaptor mod for CSS::Adaptor.
    package CSS::Adaptor::XML; $VERSION = 1.00; use CSS::Adaptor; @ISA = ('CSS::Adaptor'); use strict; use warnings; sub output_rule { my ($self, $rule) = @_; #loop on each selector foreach ($rule->selectors(@_)){ #grab the selector name my $selector = $rule->selectors; print "\t\<style name=\"".$selector."\"\>\n"; #loop on each selector's properties foreach ($rule->properties(@_)){ my @properties = $rule->properties; foreach my $line (@properties){ my %properties = split (/[:;]\s+/,$line); print "\t\t\<property name=\"".$_."\"\>".$properties{$ +_}."\<\\property\>\n" for ( keys %properties ); } } #closing style print "\t\<\\style\>\n"; } } 1; __END__
    Then I call it like so:
    my $css = CSS->new ({'adaptor' => 'CSS::Adaptor::XML'}); $css->read_string($css-passed-as-string); print $css->output(); $css->purge();
    It gives me the css file in an xml format, which is what I was looking for. I would always be grateful for any suggestions to make this code better, stronger, faster. Thanks again for your help monks :)