简体   繁体   中英

Stripping/Substitution with Perl Regex

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.

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.

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

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.

不知道为什么没有提到它,但是大括号在正则表达式中有特殊含义,因此需要转义。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM