summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornagachika <nagachika@ruby-lang.org>2022-03-12 16:00:02 +0900
committernagachika <nagachika@ruby-lang.org>2022-03-12 16:00:02 +0900
commit4b1cee1431b44e923611c65a8ec5cc61d4025641 (patch)
tree8fa67df9451a23fd24fe96520f7a390346daa137
parent3ce60f44b8de3aabb31c37d2afd2a622a64a3e6f (diff)
merge revision(s) e2ec97c4b823a0b2e0c31e7a6d77b1dcdc0dfada: [Backport #18415]
[DOC] How to get the longest last match [Bug #18415] --- string.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-)
-rw-r--r--string.c32
-rw-r--r--version.h2
2 files changed, 32 insertions, 2 deletions
diff --git a/string.c b/string.c
index 1777125b2b..4d55406dea 100644
--- a/string.c
+++ b/string.c
@@ -3846,7 +3846,6 @@ rb_str_rindex(VALUE str, VALUE sub, long pos)
return str_rindex(str, sub, s, pos, enc);
}
-
/*
* call-seq:
* string.rindex(substring, offset = self.length) -> integer or nil
@@ -3866,6 +3865,23 @@ rb_str_rindex(VALUE str, VALUE sub, long pos)
* '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:
* 'foo'.rindex('o', 0) # => nil
@@ -10101,6 +10117,20 @@ rb_str_partition(VALUE str, VALUE sep)
* "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
diff --git a/version.h b/version.h
index 55dcb6531a..c58e649e5a 100644
--- a/version.h
+++ b/version.h
@@ -12,7 +12,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 4
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
-#define RUBY_PATCHLEVEL 183
+#define RUBY_PATCHLEVEL 184
#define RUBY_RELEASE_YEAR 2022
#define RUBY_RELEASE_MONTH 3