bug is ruby regexp

Discussion in 'Ruby' started by Nick Black, Feb 2, 2007.

  1. Nick Black

    Nick Black Guest

    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
     
    Nick Black, Feb 2, 2007
    #1
    1. Advertising

  2. 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
     
    James Edward Gray II, Feb 2, 2007
    #2
    1. Advertising

  3. 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
     
    Rob Biedenharn, Feb 2, 2007
    #3
  4. Nick Black

    Guest

    On 2/2/07, James Edward Gray II <> 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
     
    , Feb 2, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Hurrell
    Replies:
    4
    Views:
    181
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    547
    Mikel Lindsaar
    Mar 31, 2008
  3. Artūras Šlajus

    A bug in Ruby regexp lib?

    Artūras Šlajus, Jan 27, 2009, in forum: Ruby
    Replies:
    3
    Views:
    108
    Artūras Šlajus
    Jan 27, 2009
  4. Joao Silva
    Replies:
    16
    Views:
    409
    7stud --
    Aug 21, 2009
  5. Uldis  Bojars
    Replies:
    2
    Views:
    218
    Janwillem Borleffs
    Dec 17, 2006
Loading...

Share This Page