Parsing Specific CSS with Regex
I'm struggling to come up right regex to solve my problem. I need to parse the variables and their values out of this Sass file. Variables always start with $ and end with :, and their values begin after the : and end with ;, much like CSS.
I'm using .Net's regex parser, and testing against this useful regex tool: http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx
I'm able to find the variables just fine with this match string
\$[\w\d-]*:
However, how can I find the matching value pair and not pick up the false positives from the other css, such as "10px"
开发者_JS百科/*
Theme Name: Huff
*/
// Particle Imports
@import "blueprint";
// Blueprint Variables
$blueprint_grid_margin: 10px;
// Site Specific Styles
$primary-color: #a1bfc2;
$link-color: #AFBDD2;
html {
font-smooth: always;
}
body.bp {
@include ui;
overflow-x: hidden;
background-color: $secondary-color;
}
You want to use groups like this.
\$([\w\d-]*):\s*(.*)?\s*;
Then match all and group 1 has the name while group 2 has the value for each match.
Try:
(\$[\w\d-]*):\s*([^;]+);
$1 will contain the variable name, $2 will contain the value. The value will include any quotes, whitespace, etc., other than whitespace that is trimmed directly after the ":".
精彩评论