Reputation: 7592
How to get (or remove) all comment lines from a matlab file?
Lines may start with no or an arbitrary number of whitespaces followed by one or more %
, followed by the comment.
Using
only_comments = regexp(raw_string, '(?m)^[ ]*[%].*?$', 'match');
fails. Also, how to make sure tabs will be catched?
As I understand this its
(?m)
line mode ^
beginning of line[ ]*
none or any number of white spaces[%].*?$
followed by a %
and then any charachter until the line end is reached.Whats wrong?
Upvotes: 0
Views: 57
Reputation: 67968
(?m)^[ ]*%+.*$
Think you need this.your regex (?m)^[ ]*[%].*?$
does not quantify %
.It will match only 1 %
.You need to use %+
to match one or more of it.
Upvotes: 1
Reputation: 174696
Seems like you want something like this,
only_comments = regexp(raw_string, '(?m)^[ ]*[%]+.*?$', 'match');
OR
only_comments = regexp(raw_string, '(?m)^ *%+.*$', 'match');
Explanation:
^
Asserts that we are at the start.<space>*
Matches zero or more spaces.%+
Matches one or more %
.*
Matches any character but not of line breaks.$
Asserts that we are at the end.Upvotes: 2