简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM