开发者

Need a regular expression to trim a css file of all values, just keep selectors

开发者 https://www.devze.com 2022-12-19 07:05 出处:网络
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.

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开发者_如何学JAVA out ready for theming from scratch.

Thanks


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消