How the regular expression to match "//"?

In C/C++ language, can use “//” to comment,such as "//i++ "

I want use ruby to distinguish the comment lines and code lines.

but fail. : (

f.each do|line|
puts line
if line =~ /^////.*/
puts “The line contain //.”
end
end

Error: Unexpected tSTRING_BEG

if line =~ /^////.*/

if line =~ /^///

if line =~ %r{^//}

(note that the .* does nothing in your original regexp, i.e. it can be
removed without changing the behaviour)

understand, thanks you very much! :slight_smile:

On Thu, Sep 16, 2010 at 4:22 PM, Brian C. [email protected]
wrote:

if line =~ /^////.*/

if line =~ /^///

if line =~ %r{^//}

(note that the .* does nothing in your original regexp, i.e. it can be
removed without changing the behaviour)

For comment lines it’s probably better to allow for some indentation:

if %r{^\s*//} =~ line
puts “This is a comment: #{line}”
end

Kind regards

robert