Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

If I have the following JSON, How can I regex match the PI: Element?

I want to extract PI : { item:1232, Item2:243... }

{
Isogen:531,
Nocxium:151,
Zydrine:0,
Megacyte:0,
Morphite:0,
PI:{
Gleaming Alloy:660,
Condensed Alloy:618,
Opulent Compound:772,
Reactive Metals:140,
Crystal Compound:719,
},
Item Name:MK5 Interdiction Spehere,
Skill Module:5/4/0,
Production Cost:600000,
Qty Per BP:3,
Material Efficiency:104
}
question from:https://stackoverflow.com/questions/66055297/how-to-match-a-json-sub-element-with-regex

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
106 views
Welcome To Ask or Share your Answers For Others

1 Answer

JSON is a nested structure, which regular expressions aren't well-suited to parse. If you know that the key you're looking for won't have any objects inside it, then you could match up until the first close brace after the key, with a regex like this:

"PI":.*?}

Note that this is not resilient at all if there's weird stuff in your json, but if you're confident that the structure is always going to be simple enough then it should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...