Overlapping regex help

(BIl Kleb) #1

We can't get pass green in the test-first's
red-green-refactor rhythm for this simple case.

We have to resort to a hacked, non-general double
gsub() invocation to get all the tests to pass:

  class String
   def transform
     gsub(/(^|\W)(abs|sign)(\s*\()/,'\1\2_c\3').
     gsub(/(^|\W)(abs|sign)(\s*\()/,'\1\2_c\3')
   end
  end

  require 'test/unit'
  class TestRegexExpression < Test::Unit::TestCase
   def test_nested_overlapping_replacement_of_functions
    assert_equal('=abs_c(sign_c(sign))',
                 '=abs(sign(sign))'.transform)
   end
   def test_only_translates_functions_of_interest
    assert_equal('stabs(sign_c(var))',
                 'stabs(sign(var))'.transform)
   end
   def test_multiple_replacements_on_a_line
     assert_equal('abs_c(varA)+abs_c(varB)',
                  'abs(varA)+abs(varB)'.transform)
   end
  end

Oh regex masters, please shine a light if you can,

···

--
Bil, http://fun3d.larc.nasa.gov

(James Edward Gray II) #2

Does this do it?

    gsub(/\b(abs|sign)(\s*\()/,'\1_c\2')

James Edward Gray II

···

On Aug 19, 2005, at 4:11 PM, Bil Kleb wrote:

We can't get pass green in the test-first's
red-green-refactor rhythm for this simple case.

We have to resort to a hacked, non-general double
gsub() invocation to get all the tests to pass:

(BIl Kleb) #3

James Edward Gray II wrote:

Does this do it?

   gsub(/\b(abs|sign)(\s*\()/,'\1_c\2')

Perfect.

Thanks,

···

--
Bil
http://fun3d.larc.nasa.gov