-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
  • 73,866
  • 12
  • 100
  • 156
nikib3ro
  • 20,366
  • 24
  • 120
  • 181

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
  • 3,226
  • 1
  • 22
  • 25