porting perl regexp to ruby

Discussion in 'Ruby' started by niklasalverup@hotmail.com, Aug 8, 2006.

  1. Guest

    I have a perl regexp that strips c++ comments (I have not constructed
    this myself, found it here:
    http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.27.html ). However, it
    uses the 'e' modifier that will take the replacement value and evaluate
    it. How do I get similar functionality in ruby?

    perl regexp below:
    s#//(.*)|/\*[^*]*\*+([^/*][^*]*\*+)*/|"(\\.|[^"\\])*"|'(\\.|[^'\\])*'|[^/"']+#
    $1 ? "/*$1 */" : $& #ge;

    /Nick
     
    , Aug 8, 2006
    #1
    1. Advertising

  2. On Aug 8, 2006, at 6:25 AM, wrote:

    > I have a perl regexp that strips c++ comments (I have not constructed
    > this myself, found it here:
    > http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.27.html ). However, it
    > uses the 'e' modifier that will take the replacement value and
    > evaluate
    > it. How do I get similar functionality in ruby?
    >
    > perl regexp below:
    > s#//(.*)|/\*[^*]*\*+([^/*][^*]*\*+)*/|"(\\.|[^"\\])*"|'(\\.|[^'\\])
    > *'|[^/"']+#
    > $1 ? "/*$1 */" : $& #ge;
    >
    > /Nick
    >
    >



    Look at the documentation for gsub. Specifically the block form.

    e.g.:

    string.gsub(/regex/) { |matched_string| code_to_evaluate }
     
    Logan Capaldo, Aug 8, 2006
    #2
    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. Mark Wilson
    Replies:
    2
    Views:
    153
    Robert Feldt
    Sep 25, 2003
  2. vasudevram
    Replies:
    0
    Views:
    235
    vasudevram
    Jul 25, 2006
  3. Mikel Lindsaar
    Replies:
    0
    Views:
    490
    Mikel Lindsaar
    Mar 31, 2008
  4. Joao Silva
    Replies:
    16
    Views:
    363
    7stud --
    Aug 21, 2009
  5. Aqua
    Replies:
    3
    Views:
    149
Loading...

Share This Page