博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

MatchData 字符匹配方法

Posted on 2010-12-06 10:57  Watir  阅读(344)  评论(0编辑  收藏  举报

mtch[i] => obj mtch[start, length] => array mtch[range] => array click to toggle source

Match Reference—MatchData acts as an array, and may be accessed using the normal array indexing techniques. mtch[0] is equivalent to the special variable $&, and returns the entire matched string. mtch[1], mtch[2], and so on return the values of the matched backreferences (portions of the pattern between parentheses).

   
   m = /(.)(.)(\d+)(\d)/.match("THX1138.")
   m[0]       #=> "HX1138"
   m[1, 2]    #=> ["H", "X"]
   m[1..3]    #=> ["H", "X", "113"]
   m[-3, 2]   #=> ["X", "113"]
一个校验正则表达式的工具: