Bug #13621
closed
String#split returns an array longer than given limit, when passing a regexp with groups
Added by cichol (Renxiang Cai) over 7 years ago.
Updated over 7 years ago.
Description
The String#split documentation(http://ruby-doc.org/core-2.4.1/String.html#method-i-split) described:
If pattern is a Regexp, str is divided where the pattern matches. Whenever the pattern matches a zero-length string, str is split into individual characters. If pattern contains groups, the respective matches will be returned in the array as well.
If limit is a positive number, at most that number of fields will be returned (if limit is 1, the entire string is returned as the only entry in an array).
However giving a regexp with groups, split can return fields more than a given limit:
'aBa'.split(/(B)()()/, 2) # => ["a", "B", "", "", "a"]
Shouldn't it be ["a", "B"]
?
- Status changed from Open to Assigned
- Assignee set to stomar (Marcus Stollsteimer)
A documentation issue.
the limit
limits only split fields, not separators.
- Status changed from Assigned to Closed
Applied in changeset trunk|r59002.
string.c: docs for String#split
- string.c: [DOC] clarify docs for String#split when called
with limit and capture groups.
Reported by Cichol Tsai. [ruby-core:81505] [Bug #13621]
- Backport changed from 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN to 2.2: REQUIRED, 2.3: REQUIRED, 2.4: REQUIRED
- Backport changed from 2.2: REQUIRED, 2.3: REQUIRED, 2.4: REQUIRED to 2.2: WONTFIX, 2.3: REQUIRED, 2.4: REQUIRED
- Backport changed from 2.2: WONTFIX, 2.3: REQUIRED, 2.4: REQUIRED to 2.2: WONTFIX, 2.3: DONE, 2.4: REQUIRED
ruby_2_3 r59227 merged revision(s) 59002.
- Backport changed from 2.2: WONTFIX, 2.3: DONE, 2.4: REQUIRED to 2.2: WONTFIX, 2.3: DONE, 2.4: DONE
ruby_2_4 r59403 merged revision(s) 59002.
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0