Search code examples
regexgrepack

finding selectors by specific css values with ack/grep regex


I have the following two selectors in my css, laid out with this formatting:

// file1.css
#myId {
    margin-right:0;
    color:#f00;
}

// file2.css
#otherId { margin-right:0; color: #f00 }

My goal is to obtain the selector and the color value using a ack/grep search.

if I:

ack-grep '(^.*color:\s+\d+)'

I'll get:

file1.css
color:#f00;

file2.css
#otherId { margin-right:0; color: #f00 }

This search worked fine for file2.css, but not so well for file1.css, probably because I use ^ to match to the beginning of a line.. how would I consistently match back to the beginning of some css selector (starting with either an id or a class or tagname)?

to be more obvious, I want:

file1.css
#myId {margin-right:0; color:#f00;

file2.css
#otherId { margin-right:0; color: #f00 }

(and the selector doesn't necessarily have to be an ID.. should I match back to previous } or beginning of file?


Solution

  • Neither grep nor ack supports multi-line regexes:

    ack FAQ

    There are other alternatives, like pcregrep. I would use some CSS parser though.