1

Has snippet of CSS:

.__chat.__32x32 {
    color: white;
    background: url({ANYNAME:anyName}Images/icons/32x32/chat.png{ANYNAME}) no-repeat;
}

The problem: need to parse out block of css(selector+rules block). Bu closing curly bracket in url considered by my pattern as closing for rules block. All the tries with lookarounds at this point did not give me a success. Question: How to make pattern consider construction {anyname} as part of the match string if it is inside url: rule or rules block?

var parser = new Regex(@"([a-z0-9\s,*\""\[\]=\.\:#_\-@]+){((?>[^}]+|(?:(?<={ROOT)|(?<={VERSION))})*)}|(\/\*(?:(?:(?!\*\/)[\S\s])+)\*\/)", RegexOptions.Multiline | RegexOptions.IgnoreCase);
            MatchCollection matches;

            matches = parser.Matches(fullContent);

            foreach (Match match in matches)
            {
                if (match.Value.IndexOf("/*") > -1)
                {
                    var cssComment = new CssComment(match.Value);
                    _cssElements.Add(cssComment);
                }
                else
                {
                    var cssBlock = new CssBlock(match.Groups[1].Value.Trim(), match.Groups[2].Value.Trim());
                    _cssElements.Add(cssBlock);
                }
            }
setty
  • 215
  • 2
  • 14
  • possible duplicate of [Regex to get string between curly braces "{I want what's between the curly braces}"](http://stackoverflow.com/questions/413071/regex-to-get-string-between-curly-braces-i-want-whats-between-the-curly-brace) – Jeredepp Oct 09 '14 at 08:07
  • What language/tool are you using? You have a nested structure. You will either need balancing groups (.net) or a recursive regex (pcre/perl). Also, at least show what you have tried... – HamZa Oct 09 '14 at 08:29
  • C# and for testing the pattern the http://regex101.com/r/tU2zB3/1 – setty Oct 09 '14 at 08:31
  • @setty regex101 uses pcre and you're using C#. This means you're using a tester that doesn't support balancing groups. I suggest you to test your regexes on http://regexhero.net and check [this answer](http://stackoverflow.com/questions/17003799/what-are-regular-expression-balancing-groups/17004406#17004406) out – HamZa Oct 09 '14 at 08:40
  • @setty you didn't show any code. So how am I supposed to know the problem you're encountering? – HamZa Oct 09 '14 at 09:13
  • @HamZa the problem is not in the code itself, until the {ANYNAME} placeholder has been added code worked correct, now the main problem is pattern, it did not parse out blocks correctly – setty Oct 09 '14 at 09:20
  • @setty again show the regex/code you're using. – HamZa Oct 09 '14 at 09:23
  • @HamZa actually http://stackoverflow.com/questions/17003799/what-are-regular-expression-balancing-groups/17004406#17004406 helped. I found http://regexstorm.net/tester?p=%28?%3a%5B%5E%7B%7D%5D%7C%28?%3COpen%3E%7B%29%7C%28?%3CContent-Open%3E%7D%29%29%2b%28?%28Open%29%28?!%29%29&i=0%20%7B1%202%20%7B3%7D%20%7B4%205%20%7B6%7D%7D%207%7D%208 case which is very similar to my but still it is too fancy – setty Oct 10 '14 at 13:29
  • Adds to many unneded groups – setty Oct 10 '14 at 13:29

1 Answers1

0

Please try this:

[.#]?[\w\s.<>]+{(\n.*)+?(?=\n}\n)\n}

it matches whole block (selector+inside code). You may have to make some alterations in the selector part [.#]?[\w\s.<>] to make it work for your problem.

Kristijan Iliev
  • 4,901
  • 10
  • 28
  • 47