How to parse a number from a string that can have a leading zero?

In ruby, I parse the date in the following format: 24092008. I want to convert each section (year, month, date) into a number.

I split them using a regular expression that creates three lines that I pass to the Integer constructor.

date =~ /^([\d]{2})([\d]{2})([\d]{4})/ year = Integer($3) month = Integer($2) day = Integer($1) 

When it hits the line of the month, it crashes as follows:

 `Integer': invalid value for Integer: "09" (ArgumentError) 

It took me a while to figure out that the interpretation of the leading zero, since Octal and 09 is not a valid Octal number (works fine with "07").

Is there an elegant solution for this, or should I just check for a number less than 10 and remove the zero first?

Thanks.

+7
string date types ruby
source share
4 answers

I am not familiar with regular expressions, so forgive me if this answer does not work. I assumed that $ 3, $ 2 and $ 1 are strings. Here is what I did in IRB to replicate the problem:

 irb(main):003:0> Integer("04") => 4 irb(main):004:0> Integer("09") ArgumentError: invalid value for Integer: "09" from (irb):4:in `Integer' from (irb):4 from :0 

But it seems that .to_i does not have the same problems:

 irb(main):005:0> "04".to_i => 4 irb(main):006:0> "09".to_i => 9 
+15
source share

Specify base 10

Tell Ruby explicitly that you want to interpret the string as a base number of 10.

 Integer("09", 10) # => 9 

This is better than .to_i if you want to be strict .

 "123abc".to_i # => 123 Integer("123abc", 10) # => ArgumentError 

As I understand it

In irb , method(:Integer) returns #<Method: Object(Kernel)#Integer> . This told me that Kernel owns this method, and I looked at the documentation on Kernel. The signature of the method shows that it takes the base as the second argument.

+5
source share

Perhaps (0([\d])|([1-9][\d])) instead of ([\d]{2}) You may have to use $ 2, $ 4 and $ 5 instead of $ 1, $ 2, $ 3.

Or if your regular expression supports (?:...) , then use (?:0([\d])|([1-9][\d]))

Since ruby ​​accepts its regular expression from perl, this latest version should work.

+1
source share

In addition, you use regex to tokenize input. Parsing (assignment of semantics) is performed in Ruby.

0
source share

All Articles