在括号之间获取文字

发布于 2025-02-04 13:12:46 字数 572 浏览 2 评论 0原文

数据

module All
{

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

输出

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

当前代码

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

我想在{}之间获取数据,此代码适用于EG。 “ {text}”,但不是因为这个字符串,也许是因为新线,我不知道太多的正则是,因此对任何帮助都表示赞赏!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

本王不退位尔等都是臣 2025-02-11 13:12:46

还使用此模式

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

,请参阅 demo

python示例

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}')

输出

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

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
    }
落花随流水 2025-02-11 13:12:46

如果我正确理解,则需要内部{}的内容。

使用此正则是可能的:\ {([[\ w \ s \ n =]*)*)\}

输入:输出:

module All
{

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

输出:


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

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
本宫微胖 2025-02-11 13:12:46

没有re.multilinere.s flags或lookarounds的另一个选项,使用被否定的字符类

\bobj[^\S\n]+(\w+)\n\s*({[^}]*})

说明

  • \ bobj与单词obj匹配,先于一个单词边界
  • [^\ s \ n]+匹配1 +没有新线的空格
  • (\ w+)捕获组1 ,匹配1+单词字符
  • \ n \ s*匹配newline和可选的whitespace char char
  • ({[^}]*})捕获组2 ,匹配可选的chars以外的其他字符然后匹配}

demo

import re

pattern = r"\bobj[^\S\n]+(\w+)\n\s*({[^}]*})"

s = ("module All\n"
            "{\n\n"
            "    obj Bucket\n"
            "    {\n"
            "        Damage = 900\n"
            "        Type = Weapon\n"
            "        Cooldown = 2\n"
            "        Craftable = false\n"
            "    }\n"
            "}")

m = re.search(pattern, s)
if m:
    print(f'"{m.group(1)}": {m.group(2)}')

regex

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

Another option without re.MULTILINE or re.S flags or lookarounds, using a negated character class.

\bobj[^\S\n]+(\w+)\n\s*({[^}]*})

Explanation

  • \bobj Match the word obj preceded by a word boundary
  • [^\S\n]+ Match 1+ spaces without newlines
  • (\w+) Capture group 1, match 1+ word characters
  • \n\s* Match a newline and optional whitespace char
  • ({[^}]*}) Capture group 2, match optional chars other than { and } and then match }

Regex demo

import re

pattern = r"\bobj[^\S\n]+(\w+)\n\s*({[^}]*})"

s = ("module All\n"
            "{\n\n"
            "    obj Bucket\n"
            "    {\n"
            "        Damage = 900\n"
            "        Type = Weapon\n"
            "        Cooldown = 2\n"
            "        Craftable = false\n"
            "    }\n"
            "}")

m = re.search(pattern, s)
if m:
    print(f'"{m.group(1)}": {m.group(2)}')

Output

"Bucket": {
        Damage = 900
        Type = Weapon
        Cooldown = 2
        Craftable = false
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文