Mostafa Talebi
Mostafa Talebi

Reputation: 9183

Regular Expression (php) to match a property in within a CSS rule

I want to write a regular expression to match (Please note that I'm not concerned with the php functions and usage, my concern is regexp and I have given the clue of php to clarify the usage):

to match a rule if it contains the property "height" in it. What I have primarily written:

^.nav+(.*)+({)+(.*)

But I do not what should I write after that... And here is our subject:

.nav,[role~=navigation]{
    clear:both;
    background:lightgrey;
    color:#5a5a5a;
    border:1px solid #c6c6c6;
    -o-border-radius:4px;
    border-radius:4px
}

.nav:after,[role~=navigation]:after{
    content:" ";
    display:block;
    height:0;
    font-size:0;
    clear:both;
    visibility:hidden
}

.nav a,.nav a:visited,.nav button{
    background:lightgrey;
    color:#5a5a5a;
    cursor:pointer;
    padding:.25em 1em;
    text-decoration:none;
    cursor:pointer;
    line-height:1.5;
    border:0;
    border-top:1px solid #c6c6c6;
    -o-border-radius:0;
    border-radius:0
}

Upvotes: 0

Views: 115

Answers (3)

rvalvik
rvalvik

Reputation: 1559

You could try something like this:

^\.nav.*?\{[^}]+(?<=[\s;])height\s*:[^}]+\}

Matches a whole .nav css rule block. (The ^\.nav.*?\{[^}]\} part) Inside of it we add a height: with a look-behind to assert that the previous character is either a ; or a whitespace. Then we need to gobble up the rest of the characters to match the whole block.

If all you want to do is find the rule blocks that have height in them, not match the whole block, then the following is sufficient:

^\.nav.*?\{[^}]+(?<=[\s;])height\s*:

Note that . has special meaning in regex, so matching .nav matches nav preceeded by any character, to match a full stop character we need to escape it: \.

Upvotes: 1

Mobin Skariya
Mobin Skariya

Reputation: 392

try this

.nav.*{.*(\bheight\s*:.*;).*}

Upvotes: 1

KristofMorva
KristofMorva

Reputation: 648

It is not tested, but probably you can see the idea from this draft:

.nav.*?{([^}]*;|\s*)height

Here we check, if height keyword is found after { or a ; (so it is a property name).

Upvotes: 1

Related Questions