Steven Ketcham
9/29/2006 11:19:00 AM
Perfect!
Thanks!
Steve
-----Original Message-----
From: Ross Bamford [mailto:rossrt@roscopeco.co.uk]
Sent: Thursday, September 28, 2006 6:00 PM
To: ruby-talk ML
Subject: Re: Regular expression reformating a value
On Fri, 2006-09-29 at 00:51 +0900, Steven Ketcham wrote:
>
> I am not really sure how to do this. I have a series of number that need
> parsed. Strip the zeros, if a caret
> exists replace it with a slash.
>
> 1.) 000123^2 - should be 123/2
> 2.) 1^2 - should be 1/2
> 3.) 0001 - should be 1
> 4.) 000123 - should be 123
Only lightly tested, but does this do what you're after?
"000123^2".gsub(/(^0+)?(\d*)(\^)?/) { "#{$2}#{'/' if $3}" }
# => "123/2"
"1^2".gsub(/(^0+)?(\d*)(\^)?/) { "#{$2}#{'/' if $3}" }
# => "1/2"
"0001".gsub(/(^0+)?(\d*)(\^)?/) { "#{$2}#{'/' if $3}" }
# => "1"
"000123".gsub(/(^0+)?(\d*)(\^)?/) { "#{$2}#{'/' if $3}" }
# => "123"
I'm assuming you mean only leading zeroes, e.g:
"0001^0001^1".gsub(/(^0+)?(\d*)(\^)?/) { "#{$2}#{'/' if $3}" }
# => "1/0001/1"
If that's not what you want, I think you could strip all the leading
zeroes with a small change:
"0001^0001^1".gsub(/(0+)?(\d*)(\^)?/) { "#{$2}#{'/' if $3}" }
# => "1/1/1"
Hope that helps,
--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk