0

以下の正規表現を使用して、次のステートメントと一致させています。

@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()));
        }
    }
4

3 に答える 3

3

探しているものを常に正規表現で示す必要があります。(?:exp)は非キャプチャ グループ()用で、 はキャプチャ グループ用です。のように名前を付けることもできます。(?<name>exp)

正規表現を次のように変更し(?:@import\surl\()(?<filename>[^.]+\.css)(?:\);)てキャプチャします

pRegexMatch.Groups["filename"].Captures[0].Value.Trim();

お役に立てれば。

よろしく

于 2012-07-14T22:02:44.707 に答える
1

代わりにグループを反復します。あなたの2番目の試合は内側のものになります。

于 2012-07-14T21:58:28.327 に答える
0

次のようにグループ名を決定する必要があります。

Regex.Matches(@"@import\surl\((?<yourGroupname)[^.]+\.css)\);"
于 2012-07-14T22:09:46.933 に答える