matching square brackets while skipping those in double quotes

24 views
Skip to first unread message

Qianqian Fang

unread,
May 8, 2015, 10:38:52 AM5/8/15
to re...@googlegroups.com
Dear regex experts:

I am writing a JSON parser in matlab. I am looking for a faster way to match array marks ([...]) while not getting confused for anything inside pairs of double quotes. 

here is a testing string:


["a", "b\\", "c\"", [ "d\\\"","e\"[" ], "f\\\"[", [ "g[\\","h]\\\"" ] ]


any tricks to match the real array brackets (there are 6 of them)
and skip the ones in the double quotes (there are 4 of them) ?

I tried the following 3 steps in matlab

s='["a", "b\\", "c\"", [ "d\\\"","e\"[" ], "f\\\"[", [ "g[\\","h]\\\"" ] ]';
s=regexprep(s,'\\\\','  ');
s=regexprep(s,'\\\"','  ');
s=regexprep(s,'(\"[^"]*\")','${char($1*0+char(32))}');

that gives

s =

[   ,      ,      , [        ,       ],         , [       ,         ] ]

it first strips the double quotes before finding the brackets. But I feel it is
not so robust (maybe fails under some strange conditions?). I am wondering 
if anyone knows a more robust approach, ideally does not involve dynamic 
regexp.

a perl or matlab sample command would be great!

thanks in advance

Qianqian


Prashant Patole

unread,
May 9, 2015, 8:57:58 AM5/9/15
to re...@googlegroups.com
in .Net i could get it by this find string

"(?:[^"\\]|\\.)*"

and replace by empty string

Prashant



--
--
Sub, Unsub, Read-on-the-web, tune your personal settings for this Regex forum:
http://groups.google.com/group/regex?hl=en

---
You received this message because you are subscribed to the Google Groups "Regex" group.
To unsubscribe from this group and stop receiving emails from it, send an email to regex+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Prashant Patole

unread,
May 9, 2015, 9:02:07 AM5/9/15
to re...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages