tags:

views:

477

answers:

4

Hi,

So I'm quite new to programming in general, so this may be a stupid question, but I am specifically trying to use regexes to strip a CSS tag. Basically I have this:

.style1 {  
    font-size: 24px;  
    font-weight: bold;  
    color: #FFEFA1;  
}

and I want it to look like this:

.style1:color:#FFEFA1

I want to maintain the style name, color attributes, and color hex, with a colon in between and no spaces. I was attempting something like the following to make this happen:

$strip =~ s/\w+\}|\w+^#([0-9a-fA-F]{3})|([0-9a-fA-F]{6})//;

but it's not working. Anyone care to set me on the right path?

Cheers.

+3  A: 

If you know that there will be a color attribute within $strip you can use

$strip =~ s!\s*{.*color:\s*(#[0-9a-f]{6});.*}!:color:$1!is;

Things to note:

  • i modifier does case insensitive matching
  • s modifier means that the '.' character matches any character including newlines
Beano
Even if you don't know if there will be a color attribute, can't you wrap the color regex in a "(color:\s*(#[0-9a-f]{6});.*})?" and then do some checking afterwards?
Benny Wong
Thanks Benny - not sure what behavior Ryan wants in the case of 'color' not being present. Ryan?
Beano
A: 

I wrote this in the plan9port environment shell, but it ports easily to any linux.

This bit of code creates a sed script to spindle your data.

#!/usr/local/plan9/bin/rc
# .style1:color:#FFEFA1
cat > this.sed <<EOF
# for lines which start with .
/\./{
# strip open curly brace
s, {,:,
# store element tag
h
# skip to next line
n
}

# strip close curly brace
/}/d

# for other lines
{
# remove spaces
s, ,,g
# get rid of ; at end
s,;$,,g
# pull back in the element tag
G
# join to one line
s,\n,,
# shift element tag to the start
# sed in plan 9 is a little different
# for gnu sed, use \( \) and \+
s,(.*)(\.[^.]+$),\2\1,
# finally print something
p
}
EOF

This bit of code runs your input against the sed script,

cat | sed -n -f this.sed <<EOF
.style1 {
font-size: 24px;
font-weight: bold;
color: #FFEFA1;
}
EOF

to generate this output.

.style1:font-size:24px
.style1:font-weight:bold
.style1:color:#FFEFA1

You can grep for lines you want, or "grep -v" the ones you don't.

Jason Catena
+3  A: 

This, like most perl answers, starts with "Use CPAN". Everything you ever wanted to do has been done before.

use CSS;

my $css = CSS->new();

$css->read_string('
.style1 {
font-size: 24px;
font-weight: bold;
color: #FFEFA1;
} 
');

$color = $css->get_style_by_selector('.style1')
             ->get_property_by_name('color')
             ->values;

Using modules like CSS from CPAN means that someone has already considered the edge cases that your regex solutions haven't. Consider:

.someClass, div.otherClass, #someid {
    color: #aa00aa
}

Getting the color using regexes for a particular selector just got a whole lot harder.

RickMeasham
A: 

Not sure why this hasn't been mentioned, but the curly bracket has a special meaning in regexes, and therefore needs to be escaped.

AmbroseChapel