tags:

views:

40

answers:

3

I have a stylesheet:

a,b,c { stuff
        lots of it
}

b { more stuff }

.test { even more }

I want a regular expression to break it up into each of the three parts, separating from '}' to '}' should work alright for my needs (except the first case obviously).

+1  A: 

In Ruby 1.9, you could

result = subject.split(/(?<=\})/)

i.e., split the string at a position following a }. Ruby 1.8 doesn't support lookbehind assertions, though, so it won't work there. And of course you'll be running into problems with nested braces, but you said that this shouldn't be a problem with your data.

In Ruby 1.8 (can't try it here), the following should work:

result = subject.split(/(\})/)

although now the closing braces won't be part of the matched elements anymore. So test {a} test2 {b} will be split into test {a, }, test2 {b, } plus an empty string.

Tim Pietzcker
Seems like a regular split('}') is the easiest way to go. Not sure about 1.9 on my host.
Dex
A: 

It may be inappropriate in your case, but you could use a CSS parser to split your CSS file by tokens and operate on them independently and harmlessly.

  1. css_parser (with this guide);
  2. CSSPool (I recommend this one just because you only need to obtain certain selectors from your CSS; this one is like a SAX parser for CSS).
floatless
The main reason I have to tokenize is because I need to leave anything that contains a body selector out of the equation. This would include a line like a, li, body, html { ... }. Not sure if these gems would make life easier or not.
Dex
A: 
.scan(/.+?\}/m)
Nakilon