Regular expression help 2

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 :slight_smile:

Thanks
Bob

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 :slight_smile:

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 :slight_smile:

On 2007-04-17, Guest [email protected] 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.

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

with

=begin
and
=end

On 4/17/07, Guest [email protected] 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