From: James Edward Gray II Date: 2007-02-03T01:03:41+09:00 Subject: Re: bug is ruby regexp On Feb 2, 2007, at 9:54 AM, Nick Black wrote: > Hello, > > I spotted this problem in ruby's regexp today: > > $ irb(main):001:0> num = "10" > => "10" > irb(main):002:0> if num =~ /[9-13]/ > irb(main):003:1> puts "hello" > irb(main):004:1> end > SyntaxError: compile error > (irb):2: invalid regular expression: /[9-13]/ > from (irb):4 > from :0 > irb(main):005:0> > > I have tested it in ruby 1.8 and 0.9. > > Anyone else spotted this? A character class ([...]) with a range of 9-1 is not valid in a regular expression because 1 does not come after 9 in your character encoding. I believe you were trying to verify that num is between 9 and 13. Your regex would not do this even if it was legal. Character classes give multiple choices for a single character, not a group of characters. Here are some ways to perform your check: >> num = "10" => "10" >> num =~ /\A(?:9|1[0123])\Z/ => 0 >> num.to_i.between? 9, 13 => true Hope that helps. James Edward Gray II