[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regexp bug in 1.9.0?

Francis Hwang

11/18/2004 3:48:00 AM

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



3 Answers

James Gray

11/18/2004 3:55:00 AM

0

On Nov 17, 2004, at 9:47 PM, Francis Hwang wrote:

> 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



nobu.nokada

11/18/2004 4:10:00 AM

0

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.

--
Nobu Nakada


Francis Hwang

11/18/2004 4:36:00 AM

0


On Nov 17, 2004, at 10:55 PM, James Edward Gray II wrote:

> On Nov 17, 2004, at 9:47 PM, Francis Hwang wrote:
>
>> 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.