Extract data between Square Brackets

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ChrisK
Forum Newbie
Posts: 9
Joined: Thu May 28, 2009 11:37 am

Extract data between Square Brackets

Post 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
Last edited by ChrisK on Mon Jan 10, 2011 3:12 pm, edited 1 time in total.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Extract data between Square Brackets

Post by McInfo »

There is a quote missing.

Code: Select all

"CEN74JA,
You might be interested in json_decode().
ChrisK
Forum Newbie
Posts: 9
Joined: Thu May 28, 2009 11:37 am

Re: Extract data between Square Brackets

Post 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().
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Extract data between Square Brackets

Post 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\[)([^]]*?)(?=\])
ChrisK
Forum Newbie
Posts: 9
Joined: Thu May 28, 2009 11:37 am

Re: Extract data between Square Brackets

Post 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\[)([^]]*?)(?=\])
Post Reply