1

I am using NotepadQQ (Linux flavour of Notepadd++) and have a CSS file that is over 5,500 lines long. My knowledge of regular expressions is limited to the very basics.

I need to search and remove everything that doesn't contain certain hex colour codes.

A sample of the CSS looks like this:

section[id*='rss-'] ul>li .rssSummary {
color:#353535;
padding-left:10px
}

.follow_me .side_body ul li a.sm {
-webkit-transition:background,.5s,ease;
-moz-transition:background,.5s,ease;
transition:background,.5s,ease;
border-radius:50%;
border:3px solid #363b37;
display:block;
height:57px;
margin:0 10px 10px 0;
text-align:center;
width:57px
}

.follow_me .side_body ul li a.sm span {
color:#363b37;
display:block;
font-family:'squared-icomoon';
font-size:40px;
height:57px;
line-height:57px;
width:57px
}

.follow_me .side_body ul li a.fb:hover {
border-color:#4c698c !important;
background:#4c698c
}

.follow_me .side_body ul li a.twitter:hover {
border-color:#00aced !important;
background:#00aced
}

But needs to be

.follow_me .side_body ul li a.sm {
border:3px solid #363b37;
}

.follow_me .side_body ul li a.sm span {
color:#363b37;
}

.follow_me .side_body ul li a.fb:hover {
border-color:#4c698c !important;
background:#4c698c
}

If a selector contains any property lines with certain colours (#363b37, #74a5bf, #6c9ab2, #6995ad, #6691a8 or #4c698c), the selector and those properties and values should remain.

If a selector does not contain any of those colour values, it should be removed in its entirety.

If a property does not contain any of those colour values, the entire line should be removed.

Is this possible using regular expressions in search and replace and if so how? TIA!

1

2 Answers 2

0

If you work on Linux, you can use the following quick and dirty solution using awk command for this task:

awk '/{/{buffer=$0; inside=1;}{if(inside){if($0 ~ /#[abcdefABCDEF0-9]+/)buffer=buffer ORS $0}else print}/}/{inside=0;print buffer ORS "}";}' input.css 
section[id*='rss-'] ul>li .rssSummary {
color:#353535;
}

.follow_me .side_body ul li a.sm {
border:3px solid #363b37;
}

.follow_me .side_body ul li a.sm span {
color:#363b37;
}

.follow_me .side_body ul li a.fb:hover {
border-color:#4c698c !important;
background:#4c698c
}

.follow_me .side_body ul li a.twitter:hover {
border-color:#00aced !important;
background:#00aced
}

Explanations:

  • /{/{buffer=$0; inside=1;} when reaching a line containing { reset the buffer and set inside to 1.
  • {if(inside){if($0 ~ /#[abcdefABCDEF0-9]+/)buffer=buffer ORS $0}else print} if inside is at 1 then check if the line contains the color code (hex only considered in the example but can be adapted to RGB), if it does you add it to the buffer, if we are not "inside" just print the line.
  • /}/{inside=0;print buffer ORS "}";} when we reach a closing bracket we print the content of the buffer
  • You can then redirect the output to another css file.
Sign up to request clarification or add additional context in comments.

Comments

0

You need to focus on { and }. It is how i understand that you need groups of strings of this pattern selector(s){properties}. The Regular Expression (.+)\{((.+):(.+))+\}. With this expression you will get an array (let suppose its name is g1) of string of matched groups of this pattern. Now in all of these groups you have to filter out the properties that lie on separate lines. You can use two types of filters

  1. line-break (\n)
  2. Semi Colon (.)

The regular expression ((.+):(.+)(\n|;))+ will give all properties inside a selector (or in other words inside a curly bracket {all properties}). Now for color patterns inside a property. The regular expression (#{1}(.)+)* will give all color values inside a property. Now check if string of this value that comes out a property inside a selector matches to any of your property values (#363b37, #74a5bf, #6c9ab2, #6995ad, #6691a8 or #4c698c) and take appropriate action as specified in your question. If color value string compare returns true for any value in your set then keep the property. if inside a selector you get a property that matches with any of color values keep the selector. Discard property that mismatch all color values and selector that have no property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.