Skip to main content

re get text between braces

Data

module All
{

    obj Bucket
    {
        Damage = 900
        Type = Weapon
        Cooldown = 2
        Craftable = false
    }
}

Output

"Bucket": {
    Damage: 900
    Type: Weapon
    Cooldown: 2
    Craftable: false
}

Current Code

def get_object(x):
    pattern = r'\{+(.*?)\}'
    
    return re.findall(pattern, x, re.MULTILINE) # returns []

I want to get the data between the { }, this code works for eg. "{ text }" but not for this string, maybe it's because of the newlines, I don't know much regex so any help is appreciated!

Answer

Use this pattern

(?<=obj\s)(\w+)(?:.*?)(\{.*?\})

Also, see the demo

Python Example

import re

text = """module All
{

    obj Bucket
    {
        Damage = 900
        Type = Weapon
        Cooldown = 2
        Craftable = false
    }
}"""

result = re.search(r"(?<=obj\s)(\w+)(?:.*?)(\{.*?\})", text, re.S)
if result:
    key, value = result.groups()
    print(f'"{key}": {value}')

Output

"Bucket": {
        Damage = 900
        Type = Weapon
        Cooldown = 2
        Craftable = false
    }
Other helpful answers

If I understand correctly, you want the contents of the inner {}.

With this regex it should be possible: \{([\w\s\n=]*)\}

Input:

module All
{

    obj Bucket
    {
        Damage = 900
        Type = Weapon
        Cooldown = 2
        Craftable = false
    }
}

Output:


        Damage = 900
        Type = Weapon
        Cooldown = 2
        Craftable = false

Comments