Mats Bakken
Mats Bakken

Reputation: 155

Using regex to find block containing a certain word

I'm trying to find blocks like these using regex. An example of the code I'm trying to find is shown bellow,

entity //This is what I want to select
{
    "id" "2"
    "classname" "light"   //This is what it should contain
    "_light" "255 255 255 200"
    "_lightHDR" "-1 -1 -1 1"
    "_lightscaleHDR" "1"
    "_quadratic_attn" "1"
    "origin" "-128 0 -128"
    editor
    {
        "color" "220 30 220"
        "visgroupshown" "1"
        "visgroupautoshown" "1"
        "logicalpos" "[0 0]"
    }
}

How would I go forward finding this block? Blocks not containing "classname" "light" should not be selected. Can anyone help me out?

Upvotes: 2

Views: 1544

Answers (1)

Bohemian
Bohemian

Reputation: 425083

Try this:

entity\s*\{[^}]*"classname" "light"[^}]*\}\s*\}

The key term here is [^}]*, which ensures the match doesn't run over to the next block.

See a live demo of this regex in action with your example and a non-matching example.

Upvotes: 4

Related Questions