MATLAB Answers


Skip commas in brackets regexp

Asked by Etienne
on 19 Apr 2017
Latest activity Commented on by Etienne
on 21 Apr 2017
Hi All
I am trying to split a string, but need to skip strings within brackets, and keep this as one entity. How can I split the following
using strsplit, so that XO(12,13,14,15) is not split at the commas. I would therefore like to ignore the content inside the brackets.
Any help would be appreciated.

  1 Comment

Is nesting of brackets permitted? If so that makes it much harder.

Sign in to comment.


1 Answer

Answer by Walter Roberson
on 19 Apr 2017
 Accepted Answer

S = 'A,VC,D,XO(12,13,14,15),DX,DY';
regexp(S,'\w+(\([^)]+\))?', 'match')


Show 1 older comment
Hi Walter
I have to say that I am very confused by how regexp works. How would I split the following:
str = 'DATA,F,[11*0.D0,4.34255D0,-1.14281D0]'
to only split by the commas outside of the brackets?
regexp(str,'\w*([\(\[][^\)\]]*[\)\]])?', 'match')
appears to work for both this case and the previous case.
Thanks :-)

Sign in to comment.