“Nick B.” email@example.com writes:
(irb):2: invalid regular expression: /[9-13]/
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.
expressions work on characters - they don’t know the number “13” only
characters 1 and 3. In your regexp, you have a character range of 9-1
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
two basic groupings - a single character ‘9’ or two characters in which
first is 1 and the second is in the range 0-3. A possible regexp could
which says match “9” or “10” or “11” or “12” or “13”, but don’t put it
match variables ((?:…). Note that you probably don’t need the ^ and $,
always like to get into the habit of using them where possible as it
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
matching strings of text - with only a couple of characters, its not
I remember seeing a post to the perl group some years ago where someone
saying that using aregexp was causing their computer to hang. However,
turned out the problem was due to not anchoring the regexp. The computer
hung, it was just taking a long long time to perform the matching. As
the expression was anchored, the “hang” was eliminated.