Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
regex for extracting css rules and double curly brackets

Currently I am separating css rules by using

preg_match_all( '/(?ims)([a-z0-9s,.:()#_\-@>*]+){([^}]*)}/', $content, $arr);

It's working fine. However, if I have double curly brackets like this

@keyframes animationName1{0%{opacity: 0}100%{opacity:1}}div{opacity:0}@keyframes animationName2{0%{opacity: 0}100%{opacity:1}}

this regex will not extract all of the animation rules due to the curly brackets within. I need suport in adapting said rule to look at the outer brackets and ignore the inner ones.

These should be the results:

1: @keyframes animationName1{0%{opacity: 0}100%{opacity:1}}
2: div{opacity:0} 
3: @keyframes animationName2{0%{opacity: 0}100%{opacity:1}}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can use

([^{}]+)({((?:[^{}]++|(\g<2>))*)})

See the regex demo. Details:

  • ([^{}]+) - Capturing group 1: one or more chars other than { and }
  • ({((?:[^{}]++|(\g<2>))*)}) - Group 2:
    • { - a { char
    • ((?:[^{}]++|(\g<2>))*) - Group 3: zero or more occurrences of any one or more chars other than { and } or Group 2 pattern (recursed)
    • } - a } char

See the PHP demo:

$text = "@keyframes animationName1{0%{opacity: 0}100%{opacity:1}}div{opacity:0}@keyframes animationName2{0%{opacity: 0}100%{opacity:1}}";
$rx = '~([^{}]+)({(?:[^{}]++|(\g<2>))*})~';
if (preg_match_all($rx, $text, $m)) {
    print_r($m[0]);
}

Output:

Array
(
    [0] => @keyframes animationName1{0%{opacity: 0}100%{opacity:1}}
    [1] => div{opacity:0}
    [2] => @keyframes animationName2{0%{opacity: 0}100%{opacity:1}}
)
over 4 years ago · Santiago Trujillo Report

0

If the format is always the same, you can also either match from 2 opening curly's till 2 closing curly's starting with an @ or match word chars followed by a single opening till closing curly.

(?s)@\w+\h+\w+\s*{[^{]*{.*?}\s*}|\w+{[^{}]+}
  • (?s) Inline modifier, make the dot match a newline (if on multiple lines)
  • @\w+\h+\w+\s* Match @ and 1+ word chars, spaces and again word chars
  • {[^{]*{ Match { then optional chars other than { and again {
  • .*? Match as least as possible chars
  • }\s*} Match }} with optional whitespace chars in between
  • | Or
  • \w+{[^{}]+} Match word chars and from {...}

Regex demo

$re = '/(?s)@\w+\h+\w+\s*{[^{]*{.*?}\s*}|\w+{[^{}]+}/';
$str = '@keyframes animationName1{0%{opacity: 0}100%{opacity:1}}div{opacity:0}@keyframes animationName2{0%{opacity: 0}100%{opacity:1}}';
preg_match_all($re, $str, $matches);

print_r($matches[0]);

Output

Array
(
    [0] => @keyframes animationName1{0%{opacity: 0}100%{opacity:1}}
    [1] => div{opacity:0}
    [2] => @keyframes animationName2{0%{opacity: 0}100%{opacity:1}}
)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!