Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > porting perl regexp to ruby

Reply
Thread Tools

porting perl regexp to ruby

 
 
niklasalverup@hotmail.com
Guest
Posts: n/a
 
      08-08-2006
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

 
Reply With Quote
 
 
 
 
Logan Capaldo
Guest
Posts: n/a
 
      08-08-2006

On Aug 8, 2006, at 6:25 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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 }


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl 5.8 and Perl 5.10 Porting - Unicode Error Aqua Perl Misc 3 04-11-2011 03:22 AM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Ruby, CSV and PDF / Porting xtopdf to Ruby with PDF::Writer vasudevram Ruby 0 07-25-2006 02:01 PM
Porting Suggestions: Lucene to Ruby; Perl Text::Balanced Mark Wilson Ruby 2 09-25-2003 06:44 PM



Advertisments