Regular expression help 2

Discussion in 'Ruby' started by Guest, Apr 17, 2007.

  1. Guest

    Guest 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

    --
    Posted via http://www.ruby-forum.com/.
     
    Guest, Apr 17, 2007
    #1
    1. Advertising

  2. Guest

    Jeremy Henty Guest

    On 2007-04-17, Guest <> 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
     
    Jeremy Henty, Apr 17, 2007
    #2
    1. Advertising

  3. Guest

    Alex Young Guest

    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
     
    Alex Young, Apr 17, 2007
    #3
  4. Guest

    John Joyce Guest

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

    with

    =begin
    and
    =end
     
    John Joyce, Apr 18, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anand

    Regular Expression help...

    Anand, Jul 9, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,231
    Eric J. Roode
    Jul 9, 2003
  2. Eric B.
    Replies:
    1
    Views:
    432
    Jim Gibson
    Dec 17, 2004
  3. VSK
    Replies:
    2
    Views:
    2,305
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    851
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    11,973
    vforvikash
    Dec 29, 2008
Loading...

Share This Page