使用 Perl Regex 进行剥离/替换

发布于 2024-07-23 11:32:47 字数 444 浏览 7 评论 0原文

所以我对一般编程很陌生,所以这可能是一个愚蠢的问题,但我特别尝试使用正则表达式来剥离 CSS 标签。 基本上我有这个:

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

我希望它看起来像这样:

.style1:color:#FFEFA1

我想维护样式名称、颜色属性和颜色十六进制,中间有冒号且没有空格。 我正在尝试类似以下的操作来实现这一点:

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

但它不起作用。 有人愿意让我走上正确的道路吗?

干杯。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦里南柯 2024-07-30 11:32:47

与大多数 Perl 答案一样,这以“Use CPAN”开头。 你想做的一切以前都已经完成了。

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;

使用 CPAN 中的 CSS 等模块意味着有人已经考虑了您的正则表达式解决方案尚未考虑到的边缘情况。 考虑一下:

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

使​​用正则表达式为特定选择器获取颜色变得更加困难。

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.

救星 2024-07-30 11:32:47

如果您知道 $strip 中会有一个颜色属性,您可以使用

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

注意事项:

  • i 修饰符不区分大小写,匹配
  • s 修饰符意味着那个'.' 字符匹配任何字符包括换行符

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
蓝海似她心 2024-07-30 11:32:47

我在 plan9port 环境 shell 中编写了这个,但它可以轻松移植到任何 Linux。

这段代码创建了一个 sed 脚本来旋转您的数据。

#!/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

这段代码根据 sed 脚本运行您的输入,

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

以生成此输出。

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

您可以 grep 查找您想要的行,或者使用“grep -v”查找您不需要的行。

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.

美人迟暮 2024-07-30 11:32:47

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文