Project

General

Profile

Actions

Bug #1312

closed

String#rindex(Regexp, offset) doesn't seem to match greedily

Added by davidkellis (David Ellis) about 16 years ago. Updated about 14 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-darwin9.6.0]
Backport:
[ruby-core:23009]

Description

=begin
I may be wrong but I was expecting:
"abcdefg ".rindex(/\w+/,10)
to return 0.

This is what actually happens:
irb(main):003:0> "abcdefg ".rindex(/\w+/,10)
=> 6
irb(main):004:0> $~
=> #<MatchData "g">
irb(main):005:0> $~[0]
=> "g"
irb(main):006:0> $~.offset(0)
=> [6, 7]

The Regexp seems to match in a non-greedy way. The /\w+/ only matches the "g". I was expecting it to be greedy and match "abcdefg" and return 0 instead of 6.

Please correct me if I'm wrong in my expectation.

Thank you,

David
=end

Actions #1

Updated by nobu (Nobuyoshi Nakada) about 16 years ago

=begin
Hi,

At Tue, 24 Mar 2009 15:16:12 +0900,
David Ellis wrote in [ruby-core:23009]:

I may be wrong but I was expecting:
"abcdefg ".rindex(/\w+/,10)
to return 0.

Wrong expectation.
String#rindex returns the rightmost position can match.

--
Nobu Nakada

=end

Actions #2

Updated by yugui (Yuki Sonoda) almost 16 years ago

  • Status changed from Open to Rejected

=begin

=end

Actions

Also available in: Atom PDF

Like0
Like0Like0