本文簡要介紹ruby語言中 MatchData.named_captures
的用法。
用法
named_captures → hash
使用命名捕獲返回 Hash
。
散列的鍵是命名捕獲的名稱。哈希值是對應組最後一次成功捕獲的字符串。
m = /(?<a>.)(?<b>.)/.match("01")
m.named_captures #=> {"a" => "0", "b" => "1"}
m = /(?<a>.)(?<b>.)?/.match("0")
m.named_captures #=> {"a" => "0", "b" => nil}
m = /(?<a>.)(?<a>.)/.match("01")
m.named_captures #=> {"a" => "1"}
m = /(?<a>x)|(?<a>y)/.match("x")
m.named_captures #=> {"a" => "x"}
相關用法
- Ruby MatchData.names用法及代碼示例
- Ruby MatchData.pre_match用法及代碼示例
- Ruby MatchData.mtch[i]用法及代碼示例
- Ruby MatchData.length用法及代碼示例
- Ruby MatchData.post_match用法及代碼示例
- Ruby MatchData.captures用法及代碼示例
- Ruby MatchData.offset用法及代碼示例
- Ruby MatchData.string用法及代碼示例
- Ruby MatchData.end用法及代碼示例
- Ruby MatchData.begin用法及代碼示例
- Ruby MatchData.match_length用法及代碼示例
- Ruby MatchData.inspect用法及代碼示例
- Ruby MatchData.match用法及代碼示例
- Ruby MatchData.values_at用法及代碼示例
- Ruby MatchData.size用法及代碼示例
- Ruby MatchData.regexp用法及代碼示例
- Ruby MatchData.to_s用法及代碼示例
- Ruby MatchData.to_a用法及代碼示例
- Ruby MatchData類用法及代碼示例
- Ruby Matrix lup()用法及代碼示例
- Ruby Matrix unitary?()用法及代碼示例
- Ruby Matrix symmetric?()用法及代碼示例
- Ruby Matrix t()用法及代碼示例
- Ruby Matrix identity()用法及代碼示例
- Ruby Matrix hash()用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 MatchData.named_captures。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。