Ruby 1.8.7 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > MatchDataクラス > captures

instance method MatchData#captures

captures -> [String]

$1, $2, ... を格納した配列を返します。

MatchData#to_a と異なり $& を要素に含みません。 グループにマッチした部分文字列がなければ対応する要素は nil になります。

/(foo)(bar)(BAZ)?/ =~ "foobarbaz"
p $~.to_a       # => ["foobar", "foo", "bar", nil]
p $~.captures       # => ["foo", "bar", nil]

[SEE_ALSO] MatchData#to_a

class MatchData