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!
CodePudding user response:
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
}
CodePudding user response:
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
CodePudding user response:
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 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}
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
}