diff options
author | nagachika <[email protected]> | 2022-03-12 16:00:02 +0900 |
---|---|---|
committer | nagachika <[email protected]> | 2022-03-12 16:00:02 +0900 |
commit | 4b1cee1431b44e923611c65a8ec5cc61d4025641 (patch) | |
tree | 8fa67df9451a23fd24fe96520f7a390346daa137 /string.c | |
parent | 3ce60f44b8de3aabb31c37d2afd2a622a64a3e6f (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(-)
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 32 |
1 files changed, 31 insertions, 1 deletions
@@ -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 |