Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: regex question

by Vautrin (Hermit)
on Mar 23, 2004 at 00:56 UTC ( #338856=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://338856]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2022-05-22 14:38 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (80 votes). Check out past polls.