0

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"

/*
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;
}
1
  • 3
    Just use an existing CSS parser? Commented Jul 20, 2011 at 4:15

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 ":".

3 Comments

When I run it against derekslager.com/blog/posts/2007/09/… I get 0 matches.
You need a * instead of a ? for the [\w\d-] class
I pasted from the wrong clipboard buffer, fixed... but looks like I'm too late.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.