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

···

--
Posted via http://www.ruby-forum.com/.

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)

···

--
Posted via http://www.ruby-forum.com/\.

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

···

On Thu, Sep 16, 2010 at 4:22 PM, Brian Candler <b.candler@pobox.com> 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)

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

understand, thanks you very much! :slight_smile:

···

--
Posted via http://www.ruby-forum.com/.