I'm trying to pull out comments in a MATLAB file. In MATLAB, comments are denoted with % so the sensible thing would be to search for %.*
. However, MATLAB also has functions like sprintf and fprintf which allow something like sprintf('x = %d', 5)
and that regex would find %d', 5)
as well, which I don't want. Of course I'd also want to ignore variations such as %s
or %f
. Is there a way to capture only those segments that match %.*
but which are not enclosed in '
characters? I suppose I should clarify that I'm generally trying to capture comments starting with %
, but ignoring any %
within string literals. The sprintf was simply an example of such an occurence that I want to ignore.
I found this question, which seems related, but no solutions posted there solve my problem.