简体   繁体   English

使用正则表达式查找包含某个单词的块

[英]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. 不应选择不包含“ classname”“ light”的块。 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. 结合示例和不匹配的示例,观看此正则表达式的实时演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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