Can anyone help me with a regex to get the first json code inside a text that contains 1 or more codes?
I’m trying to use the following code: {([^’]+)}
But this get all the jsons inside the code
All files that I want to get the first code in json have one or more blocks of code, and I need to get only the first one, which is the code after the word "Item Definition"
Text with 2 more jsons:
ItemDefinition
{ // I'm trying to get the code from here
"itemid": 590532217,
"shortname": "ammo.nailgun.nails",
"displayName": {
"token": "ammo.nailgun.nails",
"english": "Nailgun Nails"
},
"displayDescription": {
"token": "ammo.nailgun.nails.desc",
"english": "Standard nailgun ammunition"
},
"iconSprite": {
"instanceID": 148798
},
"category": 8,
"selectionPanel": 0,
"maxDraggable": 0,
"itemType": 1,
"amountType": 0,
"occupySlots": 0,
"stackable": 64,
"quickDespawn": false,
"rarity": 0,
"spawnAsBlueprint": false,
"inventorySelectSound": {
"instanceID": 115050
},
"inventoryGrabSound": {
"instanceID": 115050
},
"inventoryDropSound": {
"instanceID": 81228
},
"physImpactSoundDef": {
"instanceID": 60196
},
"condition": {
"enabled": false,
"max": 0.0,
"repairable": false,
"maintainMaxCondition": false,
"foundCondition": {
"fractionMin": 1.0,
"fractionMax": 1.0
}
},
"hidden": false,
"flags": 0,
"steamItem": {
"instanceID": 0
},
"Parent": {
"instanceID": 0
},
"worldModelPrefab": {
"guid": "d8c823436ceda0f42a4da54a972807bf"
},
"Traits": 8,
"panel": {
"instanceID": 0
}
} // Until here
ItemModProjectile
{
...other json code
}
You can use this function to extract the first JSON block, and parse it:
Output:
Explanation:
^
– start of string[^\{]*
– scan until just before the first{
(
– start of capture group[\s\S]*?
– scan cautiously over all chars until:[\r\n]\}
– a}
at the beginning of line)
– end of capture group[\s\S]*$
– scan over everything else until end of line