Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > bug is ruby regexp

Reply
Thread Tools

bug is ruby regexp

 
 
Nick Black
Guest
Posts: n/a
 
      02-02-2007
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?





--
Nick Black
--------------------------------
http://www.blacksworld.net

 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      02-02-2007
On Feb 2, 2007, at 9:54 AM, Nick Black wrote:

> 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?


A character class ([...]) with a range of 9-1 is not valid in a
regular expression because 1 does not come after 9 in your character
encoding.

I believe you were trying to verify that num is between 9 and 13.
Your regex would not do this even if it was legal. Character classes
give multiple choices for a single character, not a group of characters.

Here are some ways to perform your check:

>> num = "10"

=> "10"
>> num =~ /\A(?:9|1[0123])\Z/

=> 0
>> num.to_i.between? 9, 13

=> true

Hope that helps.

James Edward Gray II

 
Reply With Quote
 
 
 
 
Rob Biedenharn
Guest
Posts: n/a
 
      02-02-2007

On Feb 2, 2007, at 11:03 AM, James Edward Gray II wrote:

> On Feb 2, 2007, at 9:54 AM, Nick Black wrote:
>
>> 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?

>
> A character class ([...]) with a range of 9-1 is not valid in a
> regular expression because 1 does not come after 9 in your
> character encoding.
>
> I believe you were trying to verify that num is between 9 and 13.
> Your regex would not do this even if it was legal. Character
> classes give multiple choices for a single character, not a group
> of characters.
>
> Here are some ways to perform your check:
>
> >> num = "10"

> => "10"
> >> num =~ /\A(?:9|1[0123])\Z/

> => 0
> >> num.to_i.between? 9, 13

> => true
>
> Hope that helps.
>
> James Edward Gray II


or with a range:

>> num = "10"

=> "10"
>> (9..13) === num.to_i

=> true
>> num = "14"

=> "14"
>> (9..13) === num.to_i

=> false

You could also have Float values
>> num = "11.4"

=> "11.4"
>> (9..13) === num.to_i

=> true
>> (9..13) === num.to_f

=> true

>> num = "13.1"

=> "13.1"
>> (9..13) === num.to_i

=> true
>> (9..13) === num.to_f

=> false

Since num.to_i is 13.

-Rob

Rob Biedenharn http://agileconsultingllc.com
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
brabuhr@gmail.com
Guest
Posts: n/a
 
      02-02-2007
On 2/2/07, James Edward Gray II <(E-Mail Removed)> wrote:
> On Feb 2, 2007, at 9:54 AM, Nick Black wrote:
> > I spotted this problem in ruby's regexp today: ...
> >
> > (irb):2: invalid regular expression: /[9-13]/
> > from (irb):4
> > from :0
> > irb(main):005:0>

>
> A character class ([...]) with a range of 9-1 is not valid in a
> regular expression because 1 does not come after 9 in your character
> encoding.


For comparative reference:

$ perl -e 'print "foo" if 9 =~ /[9-13]/'
Invalid [] range "9-1" in regex; marked by <-- HERE in m/[9-1 <-- HERE
3]/ at -e line 1.

$ python -c "import re; re.match('[9-13]', '9')"
Traceback (most recent call last):
File "<string>", line 1, in ?
File "/usr/lib/python2.4/sre.py", line 129, in match
return _compile(pattern, flags).match(string)
File "/usr/lib/python2.4/sre.py", line 227, in _compile
raise error, v # invalid expression
sre_constants.error: bad character range

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
A bug in Ruby regexp lib? Artūras Šlajus Ruby 3 01-27-2009 07:59 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:59 PM



Advertisments