MATLAB: How to match from 2 square brackets till end using regular expression

regexp regexprep

I want to replace a string starting from 2 square brackets till end of the brackets. For example: s1 = '{[1,2],[3,4],[[5,6],[7,8]]}'; s2 = '[9,10],[11,12]'; Need to place from [5,6],[7,8]] with [9,10],[11,12] expected result: s3 = '{[1,2],[3,4],[[9,10],[11,12]]}' Anyone can support to build the regular expression?

Best Answer

Something like this,
s1 = '{[1,2],[3,4],[[5,6],[7,8]]}';
s2 = '[9,10],[11,12]';
s3 = regexprep(s1,'(?<=\[)(\[)[^)]*(\])(?=\])',s2)
s3 =
'{[1,2],[3,4],[[9,10],[11,12]]}'