From bd3283338250827e0f9e2fd1785bd1fd4151e66d Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Fri, 26 Jul 2019 14:26:59 -0700 Subject: [PATCH] Document behavior when mixing named captures with parentheses [ci skip] Fixes [Bug #13716] --- doc/regexp.rdoc | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/doc/regexp.rdoc b/doc/regexp.rdoc index 9218a75b67..40c73952d6 100644 --- a/doc/regexp.rdoc +++ b/doc/regexp.rdoc @@ -238,7 +238,15 @@ where _name_ is the group name. #=> # *Note*: A regexp can't use named backreferences and numbered -backreferences simultaneously. +backreferences simultaneously. Also, if a named capture is used in a +regexp, then parentheses used for grouping which would otherwise result +in a unnamed capture are treated as non-capturing. + + /(\w)(\w)/.match("ab").captures # => ["a", "b"] + /(\w)(\w)/.match("ab").named_captures # => {} + + /(?\w)(\w)/.match("ab").captures # => ["a"] + /(?\w)(\w)/.match("ab").named_captures # => {"c"=>"a"} When named capture groups are used with a literal regexp on the left-hand side of an expression and the =~ operator, the captured text is