Class MatchData
In: lib/oniguruma.rb
Parent: Object

Methods

[]   begin   end   offset   to_index  

External Aliases

[] -> old_aref
begin -> old_begin
end -> old_end
offset -> old_offset

Public Instance methods

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 = ORegexp.new( '(.)(.)(\d+)(\d)' ).match("THX1138.")
   m[0]       #=> "HX1138"
   m[1, 2]    #=> ["H", "X"]
   m[1..3]    #=> ["H", "X", "113"]
   m[-3, 2]   #=> ["X", "113"]

If a symbol is used as index, the corresponding named group is returned, or nil if such a group does not exist.

   m = ORegexp.new( '(?<begin>^.*?)(?<middle>\d)(?<end>.*)' ).match("THX1138")
   m[:begin]  #=> "THX"
   m[:moddle]  #=> "1"
   m[:end]  #=> "138"

Returns the offset of the start of the nth element of the match array in the string.

   m = ORegexp.new( '(.)(.)(\d+)(\d)' ).match("THX1138.")
   m.begin(0)   #=> 1
   m.begin(2)   #=> 2

If no arguments are given, the index of the first matching character is returned.

   m = ORegexp.new( '(.)(.)(\d+)(\d)' ).match("THX1138.")
   m.begin      #=> 1

If the argument is a symbol, then the beginning of the corresponding named group is returned, or nil if the group does not exist.

   m = ORegexp.new( '(?<begin>^.*?)(?<middle>\d)(?<end>.*)' ).match("THX1138")
   m.begin(:middle) #=> 3

Returns the offset of the character immediately following the end of the nth element of the match array in the string.

   m = ORegexp.new( '(.)(.)(\d+)(\d)' ).match("THX1138.")
   m.end(0)   #=> 7
   m.end(2)   #=> 3

If no arguments are given, the index of the last matching character is returned.

   m = ORegexp.new( '(.)(.)(\d+)(\d)' ).match("THX1138.")
   m.last      #=> 7

If the argument is a symbol, then the beginning of the corresponding named group is returned, or nil if the group does not exist.

   m = ORegexp.new( '(?<begin>^.*?)(?<middle>\d)(?<end>.*)' ).match("THX1138")
   m.end(:middle) #=> 4

Returns a two-element array containing the beginning and ending offsets of the nth match.

   m = ORegexp.new( '(.)(.)(\d+)(\d)' ).match("THX1138.")
   m.offset(0)   #=> [1, 7]
   m.offset(4)   #=> [6, 7]

If no arguments are given, the offsets of the entire sequence are returned.

   m = ORegexp.new( '(.)(.)(\d+)(\d)' ).match("THX1138.")
   m.offset      #=> [1, 7]

If the argument is a symbol, then the offsets of the corresponding named group are returned, or nil if the group does not exist.

   m = ORegexp.new( '(?<begin>^.*?)(?<middle>\d)(?<end>.*)' ).match("THX1138")
   m.end(:middle) #=> [3, 4]

Returns the group index for the corresponding named group, or nil if the group does not exist.

   m = ORegexp.new( '(?<begin>^.*?)(?<middle>\d)(?<end>.*)' ).match("THX1138")
   m.to_index[:begin]    #=> 1
   m.to_index[:unknown]  #=> nil

[Validate]