用正则表达式解析特定的CSS

时间:2011-07-20 04:04:23

标签: c# .net css regex

我正在努力提出正确的正则表达式来解决我的问题。我需要从这个Sass文件中解析变量及其值。变量总是以$开头并以:结束,并且它们的值在:和...结束后开始,就像CSS一样。

我正在使用.Net的正则表达式解析器,并针对这个有用的正则表达式工具进行测试:http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx

我可以使用此匹配字符串

找到变量
\$[\w\d-]*:

但是,如何找到匹配的值对而不从其他css中获取误报,例如“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;
}

2 个答案:

答案 0 :(得分:2)

您想要使用这样的群组。

\$([\w\d-]*):\s*(.*)?\s*;

然后匹配all,组1具有名称,而组2具有每个匹配的值。

答案 1 :(得分:0)

尝试:

(\$[\w\d-]*):\s*([^;]+);

$ 1将包含变量名称,$ 2将包含该值。该值将包括任何引号,空格等,而不是直接在“:”之后修剪的空格。

相关问题