在括号之间获取文字
数据
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
还使用此模式
,请参阅 demo
python示例
输出
Use this pattern
Also, see the demo
Python Example
Output
如果我正确理解,则需要内部{}的内容。
使用此正则是可能的:
\ {([[\ w \ s \ n =]*)*)\}
输入:输出:
输出:
If I understand correctly, you want the contents of the inner {}.
With this regex it should be possible:
\{([\w\s\n=]*)\}
Input:
Output:
没有
re.multiline
或re.s
flags或lookarounds的另一个选项,使用被否定的字符类。说明
\ bobj
与单词obj
匹配,先于一个单词边界[^\ s \ n]+
匹配1 +没有新线的空格(\ w+)
捕获组1 ,匹配1+单词字符\ n \ s*
匹配newline和可选的whitespace char char({[^}]*})
捕获组2 ,匹配可选的chars以外的其他字符然后匹配}
demo
regex
Another option without
re.MULTILINE
orre.S
flags or lookarounds, using a negated character class.Explanation
\bobj
Match the wordobj
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
Output