Found a bug, the [^Z] in the last caputre group should be a [^\Z] (or you
prefer, you could just swap it out with .* I don't know if it makes a
difference, I just usually try to match based on the next thing I want to
hit, in this case it's the end of the string).
Here is another version, it does the same thing, but I think it's prettier.
I swapped out the plusses for << because they're much quicker when you don't
need a new object.
def digits_only(str)
str.gsub /[^0-9]/ , ''
end
def clean_string(str)
str =~ /\A([-+]?)([eE]?)([^eE.]*)(\.?)([^eE]*)((?:[eE][+-]?)?)([^\Z]*)\Z/
$1 << digits_only($3) << $4 << digits_only($5) << $6 << digits_only($7)
end
And here is the same thing, but it assigns them to variables first. It's
uglier, but if you have to sort through it later, it can be nice to know
what the regex is supposed to be capturing.
def digits_only(str)
str.gsub /[^0-9]/ , ''
end
def clean_string(str)
str =~ /\A([-+]?)([eE]?)([^eE.]*)(\.?)([^eE]*)((?:[eE][+-]?)?)([^\Z]*)\Z/
posneg , misplaced_e , before_dec , dec , after_dec , e ,
exponent =
$1 , $2 , digits_only($3) , $4 , digits_only($5) , $6 ,
digits_only($7)
$1 << digits_only($3) << $4 << digits_only($5) << $6 << digits_only($7)
end
···
On Wed, Apr 14, 2010 at 9:58 AM, Josh Cheek <josh.cheek@gmail.com> wrote:
On Wed, Apr 14, 2010 at 8:33 AM, Alex DeCaria < > alex.decaria@millersville.edu> wrote:
Josh Cheek wrote:
> This is what I have so far, please check and correct any tests that
> should
> be different
Josh,
Your code works great! I knew there had to be a more elegant way to do
this rather than my brute force method.
The only test it didn't seem to work on was eliminating extra + or -
signs, such as '+45-2+8' => '+4528', but now that I see what you are
doing I can probably figure out how to do that. I definitely need to
learn more about regular expressions!
Thanks for your time and effort.
--Alex
--
Posted via http://www.ruby-forum.com/.
It wasn't done, because I wanted clarification on the tests first.
Anyway, this one passes all tests.
def clean_string(str)
str =~ /\A([-+]?)([eE]?)([^eE.]*\.?)([^eE]*)((?:[eE][+-]?)?)([^Z]*)\Z/
posneg , misplaced_e , before_dec , after_dec , e , exponent = $1 , $2 ,
$3 , $4 , $5 , $6
posneg + before_dec.gsub(/[^0-9.]/,'') + after_dec.gsub(/[^0-9]/,'') + e
+ exponent.gsub(/[^0-9]/,'')
end
require 'test/unit'
class TestCleanString < Test::Unit::TestCase
def test_delete_chars
assert_equal '-24.5e45' , clean_string('-24.5fge4x5')
end
def test_delete_extra_decimal
assert_equal '2.45' , clean_string('2.4.5')
assert_equal '2.45' , clean_string('2..45')
assert_equal '2.45' , clean_string('2...45')
end
def test_delete_extra_decimal_in_exponent
assert_equal '245e76' , clean_string('245e7.6')
end
def test_delete_extra_or_misplaced_pos_and_neg_signs
assert_equal '+4568e+45' , clean_string('+45-68+e+45-')
end
def test_delete_extra_or_misplaced_e_or_E
assert_equal '4.67e67' , clean_string('4.67e6e-7')
assert_equal '+4.67e-7' , clean_string('+e4.67e-7')
end
end