From: Tim X Date: 2007-02-03T13:55:08+09:00 Subject: Re: bug is ruby regexp "Nick Black" writes: > 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? > Its not a bug. The problem is you are mixing up characters and numbers. Regular expressions work on characters - they don't know the number "13" only the characters 1 and 3. In your regexp, you have a character range of 9-1 and 3. However, 9 is greater than 1, so the range doesn't make sense. Assuming you want to match on only the numbers 9, 10, 11, 12 and 13, you have two basic groupings - a single character '9' or two characters in which the first is 1 and the second is in the range 0-3. A possible regexp could therefore be /^(?:9|1[0-3])$/ which says match "9" or "10" or "11" or "12" or "13", but don't put it into the match variables ((?:...). Note that you probably don't need the ^ and $, but I always like to get into the habit of using them where possible as it anchors the regexp. If you don't anchor a regexp, you can get really really bad performance due to loads of backtracking. However, this is more applicable when matching strings of text - with only a couple of characters, its not really and issue. I remember seeing a post to the perl group some years ago where someone was saying that using aregexp was causing their computer to hang. However, it turned out the problem was due to not anchoring the regexp. The computer wasn't hung, it was just taking a long long time to perform the matching. As soon as the expression was anchored, the "hang" was eliminated. HTH Tim -- tcross (at) rapttech dot com dot au