alcybery
alcybery

Reputation: 55

Regex match text followed by curly brackets

I have a text like this:

"entity"
{
    "id" "5040044"
    "classname" "weapon_defibrillator_spawn"
    "angles" "0 0 0"
    "body" "0"
    "disableshadows" "0"
    "skin" "0"
    "solid" "6"
    "spawnflags" "3"
    "origin" "449.47 5797.25 2856"
    editor
    {
        "color" "0 0 200"
        "visgroupshown" "1"
        "visgroupautoshown" "1"
        "logicalpos" "[-13268 14500]"
    }
}

What would regex expression be to select only that part in Notepad++:

editor
{
    "color" "0 0 200"
    "visgroupshown" "1"
    "visgroupautoshown" "1"
    "logicalpos" "[-13268 14500]"
}

First word is always "editor", but the number of lines and content in curly brackets may vary.

Upvotes: 2

Views: 597

Answers (2)

OnlineCop
OnlineCop

Reputation: 4069

The simplest way to find everything between curly brackets would be \{[^{}]*\} (example 1).

You can prepend editor\s* on it so it limits the search to only that specific entry: editor\s*\{[^{}]*\} (example 2).

However... if any of the keys or value strings within editor {...} contain a { or }, you're going to have edge cases.

You'll need to find double-quoted values and essentially ignore them. This example shows how you would stop before the first double quote within the group, and this example shows how to match up through the first key-value pair.

You essentially want to repeatedly match those key-value pairs until no more remain.

If your keys or values can contain \" within them, such as "help" "this is \"quoted\" text", you need to look for that \ character as well.

If there are nested groups within this group, you'll need to recursively handle those. Most regex (Notepad++ included) don't handle recursion, though, so to get around this, you copy-paste what you have so far inside of the code if it happens to come across more nested { and }. This does not handle more than one level of nesting, though.

TL;DR

For Notepad++, this is a single line regex you could use.

Upvotes: 1

Nandee
Nandee

Reputation: 638

editor\s*{\s*(?:\"[a-z]*\"\s*\".*\"\s*)*\}

Demo
Also tested it in Notepad++ it works fine

Upvotes: 2

Related Questions