Reputation: 23205
I'm attempting (and failing) to specify a single regex which I can use with PHP's preg_match_all()
for the following:
.foo { bar }
.baz, .bot { bip, bop }
I need to count all {
and only ,
which are not between {}. Given the sample above, I should have exactly three matches. My difficulty (ignorance) is that I do not understand how to specify the "do not match commas between curly braces" part. My current regex matches all commas and opening curly braces:
({)*(,)*
Upvotes: 2
Views: 729
Reputation: 1764
If you want a close but not exact count this is a quick and dirty way to do so
$selector_count = substr_count($css, ',') + substr_count($css, '{') - substr_count($css, '@media');
It will count any ,
in the css, so rules like rgba(0, 0, 0, 0.5)
that use ,
will be counted as well. This can be useful if you know about the limitations. The accepted solution doesn't work with @media
.
Upvotes: 1
Reputation: 7546
Try this:
\{.+?\}|,
Meaning:
\{ # If you can match a brace
.+? # then also grab the minimum amount of other charactors
\} # until you reach the closing brace
|, # or if there was no brace then just match a comma
Upvotes: 2