我正在使用下面的正则表达式来匹配以下语句:
@import url(normalize.css); @import url(style.css); @import url(helpers.css);
/// <summary>
/// The regular expression to search files for.
/// </summary>
private static readonly Regex ImportsRegex = new Regex(@"@import\surl\(([^.]+\.css)\);", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
这与我的陈述相匹配,但是当我试图让这些组脱离我的比赛时,我得到的是完整的结果,而不是我期望的值。
例如预期结果normalize.css
实际结果@import url(normalize.css);
执行此操作的代码如下。谁能告诉我我做错了什么?
/// <summary>
/// Parses the string for css imports and adds them to the file dependency list.
/// </summary>
/// <param name="css">
/// The css to parse.
/// </param>
private void ParseImportsToCache(string css)
{
GroupCollection groups = ImportsRegex.Match(css).Groups;
// Check and add the @import params to the cache dependancy list.
foreach (string groupName in ImportsRegex.GetGroupNames())
{
// I'm getting the full match here??
string file = groups[groupName].Value;
List<string> files = new List<string>();
Array.ForEach(
CSSPaths,
cssPath => Array.ForEach(
Directory.GetFiles(
HttpContext.Current.Server.MapPath(cssPath),
file,
SearchOption.AllDirectories),
files.Add));
this.cacheDependencies.Add(new CacheDependency(files.FirstOrDefault()));
}
}