porting perl regexp to ruby

N

niklasalverup

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
 
L

Logan Capaldo

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 }
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top