Page 1 of 1

Extract data between Square Brackets

Posted: Mon Jan 10, 2011 2:26 pm
by ChrisK
Hi All,

I am trying to extract data between square brackets using the following expression

Code: Select all

(?<="all"\s:\s\[)([^])*?(?=\])
from the below data.

Code: Select all

itemInfo({
   "session_id" : "179-1872908",
   "continueRequests" : "1",   
   "base_interval" : "10000",
   "continue_requests" : "1",
   "filters" : {
      "all" : [
         "A1JWII2",
         "B15LQGV",
         "A3HB0IW",
         "B1EQGBL",
         "CEN74JA",
         "A372QTX",
         "D25WOG5",
         "A1RHIC9"
      ]
   },
   "ordering" : [
      {
         "__type" : "itemOrder:model"
      }
   ],
   "*classHierarchy*" : [
      "itemWS.model.ResponseMetadata"
   ]
})
When i perform the above regex with the below data @ http://gskinner.com/RegExr/ it is working. But when i call it in the PHP program, i am encountering the "missing terminator ]".

Any help or suggestions are appreciated.

Thanks
Chris

Re: Extract data between Square Brackets

Posted: Mon Jan 10, 2011 3:04 pm
by McInfo
There is a quote missing.

Code: Select all

"CEN74JA,
You might be interested in json_decode().

Re: Extract data between Square Brackets

Posted: Mon Jan 10, 2011 3:12 pm
by ChrisK
it is my mistake. While formatting the above post, i missed quote string above. Thank you.
McInfo wrote:There is a quote missing.

Code: Select all

"CEN74JA,
You might be interested in json_decode().

Re: Extract data between Square Brackets

Posted: Mon Jan 10, 2011 3:16 pm
by McInfo
If you still want to use regex instead of json_decode(), here is the old pattern followed by a corrected pattern.

Code: Select all

(?<="all"\s:\s\[)([^])*?(?=\])
(?<="all"\s:\s\[)([^]]*?)(?=\])

Re: Extract data between Square Brackets

Posted: Mon Jan 10, 2011 3:32 pm
by ChrisK
Wonderful. Thank you so much for your help.
McInfo wrote:If you still want to use regex instead of json_decode(), here is the old pattern followed by a corrected pattern.

Code: Select all

(?<="all"\s:\s\[)([^])*?(?=\])
(?<="all"\s:\s\[)([^]]*?)(?=\])