How can I make it to completely skip the commented out line?
"Skipping lines" is not an inherent capability of regular expressions. Regexps either do or do not match whatever input they are given. The line skipping logic merely can be (and in Emacs world, usually is) built around regular expressions. Without further examples of your code or intent I cannot give a complete or accurate answer.
Having said that, here is some sample code which accumulates a list of all non-empty lines in a buffer which do not begin with a comment:
(save-excursion
(goto-char (point-min))
(let (lines)
(while (< (point) (point-max))
(when (re-search-forward (rx bol (not (syntax comment-start)))
(line-end-position)
t)
(push (buffer-substring-no-properties
(line-beginning-position)
(line-end-position))
lines))
(forward-line))
(nreverse lines)))
The rx
form above which determines such lines expands to the regexp "^\\S<"
. The lines are guaranteed to be non-empty by bounding the re-search-forward
to (line-end-position)
. An unbounded search would include empty lines.
Evaluating the code above in a buffer with the following contents
foo
;; bar
baz
alice ; bob
results in the list of strings
("foo" "baz" "alice ; bob")
Here is another example which accumulates a list of all non-empty strings between the beginning of each line and either the start of a comment or the end of the line, whichever appears first:
(save-excursion
(goto-char (point-min))
(let (lines)
(while (< (point) (point-max))
(when (re-search-forward (rx bol (group (+ (not (syntax comment-start)))))
(line-end-position)
t)
(push (match-string-no-properties 1) lines))
(forward-line))
(nreverse lines)))
The rx
form used in this case expands to "^\\(\\S<+\\)"
and evaluating the code in the same buffer as before gives
("foo" "baz" "alice ")