# How the regular expression to match "//"?

**URL:** <https://www.ruby-forum.com/t/how-the-regular-expression-to-match/195081>\
**Category:** Ruby\
**Created:** [September 16, 2010, 4:20pm UTC](https://www.ruby-forum.com/t/how-the-regular-expression-to-match/195081 "2010-09-16T16:20:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![imelody](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e274bd/32.png) [@imelody](https://www.ruby-forum.com/u/imelody)\
**Post date:** [September 16, 2010, 4:20pm UTC](https://www.ruby-forum.com/t/how-the-regular-expression-to-match/195081/1 "2010-09-16T16:20:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![imelody](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e274bd/32.png) [@imelody](https://www.ruby-forum.com/u/imelody)\
**Post date:** [September 16, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/how-the-regular-expression-to-match/195081/2 "2010-09-16T16:22:26Z")

</div>

> 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)

---

<div class="post-metadata">

**Author:** ![imelody](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e274bd/32.png) [@imelody](https://www.ruby-forum.com/u/imelody)\
**Post date:** [September 16, 2010, 4:38pm UTC](https://www.ruby-forum.com/t/how-the-regular-expression-to-match/195081/3 "2010-09-16T16:38:15Z")

</div>

understand, thanks you very much! 🙂

---

<div class="post-metadata">

**Author:** ![imelody](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e274bd/32.png) [@imelody](https://www.ruby-forum.com/u/imelody)\
**Post date:** [September 16, 2010, 4:36pm UTC](https://www.ruby-forum.com/t/how-the-regular-expression-to-match/195081/4 "2010-09-16T16:36:47Z")

</div>

On Thu, Sep 16, 2010 at 4:22 PM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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
