Project

General

Profile

Bug #17113

/\K/ in separator for String#split behaves differently than /(?<=)/

Added by hanazuki (Kasumi Hanazuki) about 2 months ago. Updated about 1 month ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.8.0dev (2020-08-11T07:51:07Z master 5af983af4f) [x86_64-linux]
[ruby-core:99553]

Description

When a String is #splitted with a pattern containing /\K/ (lookbehind) operator,
the portion that matches the lookbehind pattern will not appear in the result.

"abcd".split(/b\Kc/)  # => ["a", "d"]
"abcd".split(/(?<=b)c/)  # => ["ab", "d"] -- expected result

In this example, since /b\Kc/ matches "c" in "abcd", the result is expected to be ["ab", "d"]. Actually ["a", "d"] is returned.
(?<=) operator seems to work expectedly.

#2

Updated by hanazuki (Kasumi Hanazuki) about 1 month ago

  • Status changed from Open to Closed

Applied in changeset git|e79cdcf61b0665d8a9bb309a607227de43e95673.


string.c(rb_str_split_m): Handle /\K/ correctly

Use BEG(0) instead of the result of rb_reg_search to handle the cases
when the separator Regexp contains /\K/ (lookbehind) operator.

Fixes [Bug #17113]

#3

Updated by nobu (Nobuyoshi Nakada) about 1 month ago

  • Backport changed from 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN to 2.5: REQUIRED, 2.6: REQUIRED, 2.7: REQUIRED

Also available in: Atom PDF