Alex LeDonne
12/6/2006 10:44:00 PM
On 12/6/06, Daniel Finnie <danfinnie@optonline.net> wrote:
> irb(main):002:0> regex = /([0-9]*)([^\+- ]+)(.*)/
> SyntaxError: compile error
> (irb):2: invalid regular expression: /([0-9]*)([^\+- ]+)(.*)/
> from (irb):2
>
> irb(main):003:0> regex = /([0-9]*)([^\+ -]+)(.*)/
> => /([0-9]*)([^\+ -]+)(.*)/
>
> The only thing that it different in the 2 regexps is the placement of
> the space in the square brackets, yet the first regexp is invalid and
> the 2nd valid.
>
> Why is this?
>
> Thanks,
> Dan
>
It's the placement of the - that makes a difference. In a character
class, - between two characters denotes a range. So the first
character class includes a range, from + to <space>, which is invalid
because + comes after space in the relevant character encoding.
If you want a literal hyphen in a character class, it's safest to make
it the last character.
-A