DevHeads.net

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

Comments

Re: How the regular expression to match "//"?

By Brian Candler at 09/16/2010 - 10:22

if line =~ /^\/\//

if line =~ %r{^//}

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

Re: How the regular expression to match "//"?

By Imelody __ at 09/16/2010 - 10:38

understand, thanks you very much! :-)

Re: How the regular expression to match "//"?

By Robert Klemme at 09/16/2010 - 10:36

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