[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regular expression help 2

Jim flip

4/17/2007 4:20:00 PM

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

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

3 Answers

Jeremy Henty

4/17/2007 4:37:00 PM

0

On 2007-04-17, Guest <james@dimsum.tv> 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 Henty

Alex Young

4/17/2007 4:39:00 PM

0

Guest wrote:
> 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 :)
>
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 :-)

--
Alex

John Joyce

4/18/2007 3:37:00 AM

0

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

with

=begin
and
=end