[前][次][番号順一覧][スレッド一覧]

ruby-changes:70323

From: Nobuyoshi <ko1@a...>
Date: Sun, 19 Dec 2021 20:27:48 +0900 (JST)
Subject: [ruby-changes:70323] e2ec97c4b8 (master): [DOC] How to get the longest last match [Bug #18415]

https://git.ruby-lang.org/ruby.git/commit/?id=e2ec97c4b8

From e2ec97c4b823a0b2e0c31e7a6d77b1dcdc0dfada Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Sun, 19 Dec 2021 20:12:26 +0900
Subject: [DOC] How to get the longest last match [Bug #18415]

---
 string.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/string.c b/string.c
index 678f63781a1..24acdfae0e4 100644
--- a/string.c
+++ b/string.c
@@ -4081,7 +4081,6 @@ rb_str_rindex(VALUE str, VALUE sub, long pos) https://github.com/ruby/ruby/blob/trunk/string.c#L4081
     return str_rindex(str, sub, s, pos, enc);
 }
 
-
 /*
  *  call-seq:
  *    rindex(substring, offset = self.length) -> integer or nil
@@ -4103,6 +4102,23 @@ rb_str_rindex(VALUE str, VALUE sub, long pos) https://github.com/ruby/ruby/blob/trunk/string.c#L4102
  *    'foo'.rindex(/oo/) # => 1
  *    'foo'.rindex(/ooo/) # => nil
  *
+ *  The _last_ match means starting at the possible last position, not
+ *  the last of longest matches.
+ *
+ *    'foo'.rindex(/o+/) # => 2
+ *    $~ #=> #<MatchData "o">
+ *
+ *  To get the last longest match, needs to combine with negative
+ *  lookbehind.
+ *
+ *    'foo'.rindex(/(?<!o)o+/) # => 1
+ *    $~ #=> #<MatchData "oo">
+ *
+ *  Or String#index with negative lookforward.
+ *
+ *    'foo'.index(/o+(?!.*o)/) # => 1
+ *    $~ #=> #<MatchData "oo">
+ *
  *  \Integer argument +offset+, if given and non-negative, specifies the maximum starting position in the
  *   string to _end_ the search:
  *
@@ -10395,6 +10411,20 @@ rb_str_partition(VALUE str, VALUE sep) https://github.com/ruby/ruby/blob/trunk/string.c#L10411
  *     "hello".rpartition("l")         #=> ["hel", "l", "o"]
  *     "hello".rpartition("x")         #=> ["", "", "hello"]
  *     "hello".rpartition(/.l/)        #=> ["he", "ll", "o"]
+ *
+ *  The match from the end means starting at the possible last position, not
+ *  the last of longest matches.
+ *
+ *     "hello".rpartition(/l+/)        #=> ["hel", "l", "o"]
+ *
+ *  To partition at the last longest match, needs to combine with
+ *  negative lookbehind.
+ *
+ *     "hello".rpartition(/(?<!l)l+/)  #=> ["he", "ll", "o"]
+ *
+ *  Or String#partition with negative lookforward.
+ *
+ *     "hello".partition(/l+(?!.*l)/)  #=> ["he", "ll", "o"]
  */
 
 static VALUE
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]