[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

porting perl regexp to ruby

niklasalverup

8/8/2006 10:23:00 AM

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/... ). 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

1 Answer

Logan Capaldo

8/8/2006 12:07:00 PM

0


On Aug 8, 2006, at 6:25 AM, niklasalverup@hotmail.com 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/... ). 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 }