Regexp bug in 1.9.0?

F

Francis Hwang

Is this behavior a bug, or am I misunderstanding how I should make a
regexp?

irb(main):001:0> str = "123.456.*"
=> "123.456.*"
irb(main):002:0> str =~ /^[0123456789.-*]+/
SyntaxError: compile error
(irb):2: empty range in char class: /^[0123456789.-*]+/
from (irb):2
irb(main):003:0> str =~ /^[0123456789*.-]+/
=> 0

Francis
 
J

James Edward Gray II

Is this behavior a bug, or am I misunderstanding how I should make a
regexp?

irb(main):001:0> str = "123.456.*"
=> "123.456.*"
irb(main):002:0> str =~ /^[0123456789.-*]+/
SyntaxError: compile error
(irb):2: empty range in char class: /^[0123456789.-*]+/
from (irb):2
irb(main):003:0> str =~ /^[0123456789*.-]+/
=> 0

- is a special character inside a Regexp character class. It means,
from this (character left of dash) to this (character right of dash).
It's usually used to say A-Z, a-z, 0-9, or similar. Putting it at the
beginning or end of a class negates this special meaning, thus the
change in the second attempt.

Did you really mean all characters from period to asterisk?

James Edward Gray II
 
N

nobu.nokada

Hi,

At Thu, 18 Nov 2004 12:47:46 +0900,
Francis Hwang wrote in [ruby-talk:120700]:
Is this behavior a bug, or am I misunderstanding how I should make a
regexp?

Not a bug. Ranges in char class must be in order.
irb(main):002:0> str =~ /^[0123456789.-*]+/
SyntaxError: compile error
(irb):2: empty range in char class: /^[0123456789.-*]+/
from (irb):2

$ ruby -e 'p ?., ?*'
46
42
irb(main):003:0> str =~ /^[0123456789*.-]+/
=> 0

'-' at the end of char class is not treated as a range.
 
F

Francis Hwang

Is this behavior a bug, or am I misunderstanding how I should make a
regexp?

irb(main):001:0> str = "123.456.*"
=> "123.456.*"
irb(main):002:0> str =~ /^[0123456789.-*]+/
SyntaxError: compile error
(irb):2: empty range in char class: /^[0123456789.-*]+/
from (irb):2
irb(main):003:0> str =~ /^[0123456789*.-]+/
=> 0

- is a special character inside a Regexp character class. It means,
from this (character left of dash) to this (character right of dash).
It's usually used to say A-Z, a-z, 0-9, or similar. Putting it at the
beginning or end of a class negates this special meaning, thus the
change in the second attempt.

Ah, of course. I fixated on the * and totally forgot about what the -
was doing. Thanks.

F.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top