1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Document behavior when mixing named captures with parentheses [ci skip]

Fixes [Bug #13716]
This commit is contained in:
Jeremy Evans 2019-07-26 14:26:59 -07:00
parent 5fef46ae0d
commit bd32833382

View file

@ -238,7 +238,15 @@ where _name_ is the group name.
#=> #<MatchData "ototo" vowel:"o">
*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 # => {}
/(?<c>\w)(\w)/.match("ab").captures # => ["a"]
/(?<c>\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 <tt>=~</tt> operator, the captured text is