RegEx搜索包含颜色代码的所有CSS行崇高文本3

时间:2021-01-16 07:33:55

I have a large file consisting of ~4,000 lines of CSS within Sublime Text. I'd like to write a RegEx to search the file for all CSS rules containing a specific color code (which is #48535B,) so I can copy each of the corresponding CSS rules out into another file, and then delete them from the original file. What would be a good RegEx to accomplish this?

我有一个大文件,由Sublime Text中的~4,000行CSS组成。我想编写一个RegEx来搜索文件,查找包含特定颜色代码(#48535B)的所有CSS规则,这样我就可以将每个相应的CSS规则复制到另一个文件中,然后从原始文件中删除它们。文件。什么是一个很好的RegEx来实现这一目标?

So for example, I have CSS rules formatted like so:

例如,我有CSS规则格式如下:

a {
    color:#000;
    text-decoration:none
}
a:hover,a:focus {
    color:#999;
    text-decoration:underline
}

.model-selector-parallax .carousel-prev,
.model-selector-parallax .carousel-next,
.ddc-btn-primary,
.showroom-detail .hproduct .view-link,
.showroom-detail .callout .callout-button,
.pricing .ePrice .ddc-btn, .pricing .eprice-button,
.pricing .ddc-btn[data-eprice],
.mycars-favorites .hproduct .pricing .make-an-offer,
.locations-proximity {
        outline:thin dotted #48535B !important;
        outline:5px auto -webkit-focus-ring-color;
        outline-offset:-2px
    }

img {
    vertical-align:middle
}
.img-responsive {
    display:block;
    max-width:100%;
    height:auto
}
.img-rounded {
    border-radius:0px
}`

I'd like the RegEx to find the entire rule which contains "#48535B", and highlight it from the beginning of the rule to the close } bracket. So in this case, it would find and highlight (including all of the comma separated classes, even if they're on different lines):

我希望RegEx找到包含“#48535B”的整个规则,并从规则的开头到close}括号突出显示它。所以在这种情况下,它会找到并突出显示(包括所有逗号分隔的类,即使它们位于不同的行上):

.model-selector-parallax .carousel-prev,
.model-selector-parallax .carousel-next,
.ddc-btn-primary,
.showroom-detail .hproduct .view-link,
.showroom-detail .callout .callout-button,
.pricing .ePrice .ddc-btn, .pricing .eprice-button,
.pricing .ddc-btn[data-eprice],
.mycars-favorites .hproduct .pricing .make-an-offer,
.locations-proximity {
        outline:thin dotted #48535B !important;
        outline:5px auto -webkit-focus-ring-color;
        outline-offset:-2px
    }

At which point, I can copy all the results and paste them into a separate file.

此时,我可以复制所有结果并将其粘贴到单独的文件中。

Also, a RegEx to avoid matching a certain color code. So for example, if we're trying to avoid matching 48535B and we have:

另外,RegEx避免匹配某个颜色代码。例如,如果我们试图避免匹配48535B,我们有:

.navbar-default {
  -webkit-box-shadow:rgba(0,0,0,0.15) 0px 10px 10px;
  -moz-box-shadow:rgba(0,0,0,0.15) 0px 10px 10px;
  box-shadow:rgba(0,0,0,0.15) 0px 10px 10px;
  border-top:solid #f2f2f2;
  border-bottom:solid #48535B !important;
  border-width:2px 0 1px
}

The RegEx would find and highlight:

RegEx将找到并强调:

.ddc-navbar-default {
  -webkit-box-shadow:rgba(0,0,0,0.15) 0px 10px 10px;
  -moz-box-shadow:rgba(0,0,0,0.15) 0px 10px 10px;
  box-shadow:rgba(0,0,0,0.15) 0px 10px 10px;
  border-top:solid #f2f2f2;
  border-width:2px 0 1px
}

Meaning that it finds and highlights everything but the line that contains 48535B, including comma separated classes on different lines.

这意味着它找到并突出显示除了包含48535B的行之外的所有内容,包括不同行上的逗号分隔类。

Any help would be greatly appreciated!

任何帮助将不胜感激!

Thank you very much!

非常感谢你!

1 个解决方案

#1


0  

I tried this one an it seems to work:

我试过这个似乎工作:

[^{\n]+\{[^}]+#[0-9a-f]{6}[^}]+\}

I'm not an expert on regular expressions, though. I hope it works for you

不过,我不是正则表达专家。我希望这个对你有用

For comma-separated multi-line rules, this one seems to work for me:

对于逗号分隔的多行规则,这个规则似乎对我有用:

^[^{}]+\{[^}]+#[0-9a-f]{6}[^}]+\}

For avoiding matching certain color code:

为避免匹配某些颜色代码:

^[^{}]+\{[^}]+#(?!code)[0-9a-f]{6}[^}]+\}

For example, this will not match ffffff:

例如,这与ffffff不匹配:

^[^{}]+\{[^}]+#(?!ffffff)[0-9a-f]{6}[^}]+\}

#1


0  

I tried this one an it seems to work:

我试过这个似乎工作:

[^{\n]+\{[^}]+#[0-9a-f]{6}[^}]+\}

I'm not an expert on regular expressions, though. I hope it works for you

不过,我不是正则表达专家。我希望这个对你有用

For comma-separated multi-line rules, this one seems to work for me:

对于逗号分隔的多行规则,这个规则似乎对我有用:

^[^{}]+\{[^}]+#[0-9a-f]{6}[^}]+\}

For avoiding matching certain color code:

为避免匹配某些颜色代码:

^[^{}]+\{[^}]+#(?!code)[0-9a-f]{6}[^}]+\}

For example, this will not match ffffff:

例如,这与ffffff不匹配:

^[^{}]+\{[^}]+#(?!ffffff)[0-9a-f]{6}[^}]+\}