From: daniel@...42.com Date: 2020-01-07T13:23:47+00:00 Subject: [ruby-core:96702] [Ruby master Bug#11014] String#partition doesn't return correct result on zero-width match Issue #11014 has been updated by Dan0042 (Daniel DeLorme). IIRC this has to do with zero-length matches being ignored in certain conditions, in particular having to do with repeating/multiple matches. if `"foo".split(/\A/)` was `["","foo"]` then `"foo".split(//)` would have to be `["","f","o","o"]` and `"foo".split(/\G/)` could result in infinite loop matching `["","","","","",..."foo"]` But I don't understand why `partition` doesn't behave like `match`. Note that gsub has different behavior: `"foo".gsub(/\G/,'_') #=> "_f_o_o_"` `"foo".gsub(//,'_') #=> "_f_o_o_"` explained better than I ever could: https://www.regular-expressions.info/zerolength.html ---------------------------------------- Bug #11014: String#partition doesn't return correct result on zero-width match https://bugs.ruby-lang.org/issues/11014#change-83688 * Author: janko (Janko Marohni��) * Status: Assigned * Priority: Normal * Assignee: matz (Yukihiro Matsumoto) * Target version: * ruby -v: ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14] * Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN ---------------------------------------- First, to see how `String#match` works on my example: ~~~ruby match = "foo".match(/^=*/) match.pre_match #=> "" match[0] #=> "" match.post_match #=> "foo" ~~~ Now, if I used `String#partition` instead of `match`, I'd expect to get `["", "", "foo"]` (pre_match, match, post_match). However ~~~ruby "foo".partition(/^=*/) #=> ["foo", "", ""] ~~~ `String#rpartition` returns the correct result (with the same regex). -- https://bugs.ruby-lang.org/ Unsubscribe: