-1

Say I have following JSON:

{"MyPerson":{"Firstname":"First","Lastname":"Last","Where":{"Street":"Street","Number":15}},"AnotherComplexObject":{"Another":"Yes","Me":"True"},"Count":1,"Start":2}

I remove starting { and ending } and get:

"MyPerson":{"Firstname":"First","Lastname":"Last","Where":{"Street":"Street","Number":15}},"AnotherComplexObject":{"Another":"Yes","Me":"True"},"Count":1,"Start":2

Now, what regex would I use to get "complex objects" out, for example in that JSON I would want to get these two results:

{"Firstname":"First","Lastname":"Last","Where":{"Street":"Street","Number":15}}
{"Another":"Yes","Me":"True"}

The closest I've came to solution is this regex {[^}]*} but that one fails to select } in that "Number":15 result.

Alan Moore
  • 71,299
  • 12
  • 93
  • 154
nikib3ro
  • 19,951
  • 21
  • 116
  • 176

1 Answers1

1
# String
# "MyPerson":{"Firstname":"First","Lastname":"Last","Where":{"Street":"Street","Number":15}},"AnotherComplexObject":{"Another":"Yes","Me":"True"},"Count":1,"Start":2

/({[^}]+}})/
# http://rubular.com/r/rupVEn9yZo
# Match Groups
# 1. {"Firstname":"First","Lastname":"Last","Where":{"Street":"Street","Number":15}}

/({[^}]+})/
# http://rubular.com/r/H5FaoH18c8
# Match Groups
# Match 1
# 1. {"Firstname":"First","Lastname":"Last","Where":{"Street":"Street","Number":15}
# Match 2
# 1. {"Another":"Yes","Me":"True"}

/({[^}]+}})[^{]+({[^}]+})/
# http://rubular.com/r/zmcyjvoR1y
# Match Groups
# 1. {"Firstname":"First","Lastname":"Last","Where":{"Street":"Street","Number":15}}
# 2. {"Another":"Yes","Me":"True"}


# String
# {"MyPerson":{"Firstname":"First","Lastname":"Last","Where":{"Street":"Street","Number":15}},"AnotherComplexObject":{"Another":"Yes","Me":"True"},"Count":1,"Start":2}

/[^{]+({[^}]+}})[^{]+({[^}]+})/
# http://rubular.com/r/qCxN1Rk9Ka
# Match Groups
# 1. {"Firstname":"First","Lastname":"Last","Where":{"Street":"Street","Number":15}}
# 2. {"Another":"Yes","Me":"True"}
SoAwesomeMan
  • 2,996
  • 1
  • 18
  • 23