Regular expression help 2

G

Guest

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
 
J

Jeremy Henty

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 ...
#!/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
Mary
had a
little
lamb
<<< Output ends

Regards,

Jeremy Henty
 
A

Alex Young

Guest said:
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 :)
 
J

John Joyce

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

with

=begin
and
=end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top