MATLAB: Help to solve a regexp problem

MATLABregular expression

I'm trying to capture comment block in a string (char array). Regular expression '%.*(\n|$)' captures string from % to the end of the line.
regexp(sprintf(' %%this is a comment'),'%.*(\n|$)','match')
ans =
cell
'% this is a comment'
However, what I want to do is to capture multiple incidence of comment blocks in a char array. The above expression only captures the first one, failing to match '% and this'.
regexp(sprintf(' %%this is a comment\n %%and this\n'),'%.*(\n|$)','match')
ans =
cell
'% this is a comment…'
Could somebody help me about this?

Best Answer

Test this
>> str = sprintf(' %%this is a comment\n %%and this\n');
>> cac = regexp( str, '[ ]*%[^\n]+', 'match' )
cac =
' % this is a comment' ' % and this'
>> whos cac
Name Size Bytes Class Attributes
cac 1x2 294 cell