embert
embert

Reputation: 7592

Get all comment lines

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

Whats wrong?

Upvotes: 0

Views: 57

Answers (2)

vks
vks

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

Avinash Raj
Avinash Raj

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

Related Questions