# Regular expression help 2

**URL:** <https://www.ruby-forum.com/t/regular-expression-help-2/92271>\
**Category:** Ruby\
**Created:** [April 17, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/regular-expression-help-2/92271 "2007-04-17T18:19:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [April 17, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/regular-expression-help-2/92271/1 "2007-04-17T18:19:56Z")

</div>

Okay had such a great response before and learned lots but how about  
another question?

have some strings such as:

blahblah /\* comment _/  
blahblah  
blah blah  
blah blah blah /_ comment etc \*/

And i want just the blahblah bit, not any comments if there are any.  
Any ideas on how to do this using a single regex would be most welcome,  
I know I’m pushing my luck 🙂

Thanks  
Bob

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [April 17, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/regular-expression-help-2/92271/2 "2007-04-17T18:40:53Z")

</div>

Guest wrote:

> And i want just the blahblah bit, not any comments if there are any.  
> Any ideas on how to do this using a single regex would be most welcome,  
> I know I’m pushing my luck 🙂

You’re best off doing this the other way around (untested):

str.gsub(//\*.\*?\*//m, ‘’)

That way you’re stripping the comments out of the string, rather than  
trying to find bits of the string that happen not to be in comments.  
I’m not sure it’s possible to simply do the latter, but I’m sure someone  
will be along in a few moments to prove me wrong 🙂

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [April 17, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/regular-expression-help-2/92271/3 "2007-04-17T18:41:54Z")

</div>

On 2007-04-17, Guest [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> have some strings such as:
> 
> blahblah /\* comment _/  
> blahblah  
> blah blah  
> blah blah blah /_ comment etc \*/
> 
> And i want just the blahblah bit, not any comments …

> > > Script starts  
> > > #!/bin/sh

ruby -wnle ‘puts %r{^(._?)(\s_/\*._\*/\s_)?$}.match($\_).captures[0]’  
\<\<EOF  
Mary /\* a girl _/  
had a  
little /_ but no less valuable \*/  
lamb  
EOF  
\<\<\< Script ends

> > > Output starts  
> > > Mary  
> > > had a  
> > > little  
> > > lamb  
> > > \<\<\< Output ends

Regards,

Jeremy H.

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [April 18, 2007, 5:36am UTC](https://www.ruby-forum.com/t/regular-expression-help-2/92271/4 "2007-04-18T05:36:58Z")

</div>

for multi-line comments, its the same thing  
just parse it a second time but replace  
/\*  
and  
\*/

with

=begin  
and  
=end

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [April 17, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/regular-expression-help-2/92271/5 "2007-04-17T18:46:55Z")

</div>

On 4/17/07, Guest [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And i want just the blahblah bit, not any comments if there are any.

str =\<\<-EOS  
blahblah /\* comment _/  
blahblah  
blah blah  
blah blah blah /_ comment etc \*/  
EOS

puts str.gsub(//\*.\*/,‘’)

This won’t work for multi-line comments unfortunately.

-Adam
