Overlapping regex help

B

Bil Kleb

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,
 
J

James Edward Gray II

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:

Does this do it?

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

James Edward Gray II
 

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

Latest Threads

Top