views:

173

answers:

2

Used to have this but lost it. Could someone assist?

Its a short reg expression that I pasted into TextMates search replace to trim a css file in this way.

It finds all text between {} and removes it.

selector { value: blah; }

Becomes..

selector {}

Its so i can clean a css file out ready for theming from scratch.

Thanks

+2  A: 

To find all text between {} and remove it, following could work (python)

>>> re.sub("(\w+)\s*\{.*?\}","\\1 {}","""selector { value: blah; }""")
'selector {}'

But if you need complicated one, you would rather search for css parsers.

S.Mark
A CSS parser is really necessary if the stylesheet is even moderately complex. There are so many edge cases where a closing curly brace might appear within the style rules, whether in strings (which are defined *in the spec* as not requiring delimiters), @media groups, and a number of browser-specific hacks.
eyelidlessness
Yes, absolutely correct. Correct answer would be css parser, but I havn't use css parsers before, so Its hard to suggest to OP which one is suitable.
S.Mark
Anyone know of a suitable CSS parser? Already googld' to no avail.
Gavin Hall
You could try to search in SO first, If no avail, raise a new question and ask for css parser, and please also mention the language.
S.Mark
+2  A: 

The pattern "{.*?}" means "curly brace followed by the shortest possible string, followed by the close curly brace".

If you are using a regular expression matcher that doesn't understand greedy and non-greedy ("*" is greedy, "*?" is non-greedy), you can use a pattern like "{[^}]*}" which means "curly brace followed by zero or more characters other than a close curly brace, followed by a close curly brace".

Note that this is not foolproof -- if you have closing curly braces as part of a definition this will break. The only way around that is to use a real .css parser.

If you want to capture the data between the curly braces you'll want to add parenthesis surrounding the part of the pattern inside the curly braces, eg: "{(.*?)}", which will capture everything except the curly braces.

Bryan Oakley
Looks similar to what i had running before, but doesnt actually work. I run it and it returns nothing.
Gavin Hall
@Gavin: The expressions in this answer merely match everything between opening and closing braces. My assumption was, if you can match it then you can easily replace what was matched with {}, but that's dependent on the tool you are using. You say you're using textmate but it wasn't clear in the question if you needed a textmate-specific answer. If so, you might want to add textmate as a tag and explictly ask "how to I replace this expressin in textmate"
Bryan Oakley
@Gavin: I don't use textmate, by my guess from scanning the documentation is that you need to put parenthesis around the part of the pattern you want to replace (eg: {(.*?)}) and then replace with an empty string.
Bryan Oakley