From 4b1cee1431b44e923611c65a8ec5cc61d4025641 Mon Sep 17 00:00:00 2001 From: nagachika Date: Sat, 12 Mar 2022 16:00:02 +0900 Subject: 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(-) --- string.c | 32 +++++++++++++++++++++++++++++++- version.h | 2 +- 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 + * $~ #=> # + * + * To get the last longest match, needs to combine with negative + * lookbehind. + * + * 'foo'.rindex(/(? 1 + * $~ #=> # + * + * Or String#index with negative lookforward. + * + * 'foo'.index(/o+(?!.*o)/) # => 1 + * $~ #=> # + * * \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(/(? ["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 -- cgit v1.2.3