I think it could be doable if the string were split into an array of the pieces. But the code would be a nightmarish bloated mess of conditionals and temporary variables compared to using regex.
As Mittineague says, regex is the best tool for the job.
BLOCK = %{
This is a line with // a comment here
This is a line with // a comment here
This is a line with /* this is the first line of comment
the second line
and the thrid line of the comment */
This is a line with no comments
}
singleline_comments = BLOCK.scan(/\/\/.*$/)
multiline_comments = BLOCK.scan(/\/\*.*?\*\//m)
p singleline_comments
// => ["// a comment here", "// a comment here"]
p multiline_comments
// => ["/* this is the first line of comment\nthe second line\nand the thrid line of the comment */"]