Class: MatchData

Inherits:
Object show all
Defined in:
lib/source/ruby.rb

Overview

MatchData is the class of the object returned by String#match, Regexp#match, and Regexp#last_match. It encapsulates all the results of a pattern match.

Instance Method Summary collapse

Constructor Details

#initializeMatchData

:nodoc:



3855
3856
3857
# File 'lib/source/ruby.rb', line 3855

def initialize # :nodoc:
  `this._captures=[]`
end

Instance Method Details

#[](*args) ⇒ Object

call-seq:

mtch[i]             -> object
mtch[start, length] -> array
mtch[range]         -> array

Match Reference – MatchData acts as an array, and may be accessed using the normal array indexing techniques. mtch[0] returns the entire matched string, while 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"]


3877
3878
3879
# File 'lib/source/ruby.rb', line 3877

def [](*args)
  `c$Array.prototype.m$_brac.apply(this._captures,args)`
end

#begin(n) ⇒ Object

FIX: Incomplete



3882
3883
# File 'lib/source/ruby.rb', line 3882

def begin(n)
end

#capturesObject

call-seq:

mtch.captures -> array

Returns the array of captures.

m = /(.)(.)(\d+)(\d)/.match("THX1138.").captures

m[0]    #=> "H"
m[1]    #=> "X"
m[2]    #=> "113"
m[3]    #=> "8"


3897
3898
3899
# File 'lib/source/ruby.rb', line 3897

def captures
  `this._captures.slice(1)`
end

#end(n) ⇒ Object

FIX: Incomplete



3902
3903
# File 'lib/source/ruby.rb', line 3902

def end(n)
end

#inspectObject

:nodoc:



3919
3920
3921
# File 'lib/source/ruby.rb', line 3919

def inspect # :nodoc:
  `c$Object.prototype.m$toS.apply(this)`
end

#lengthObject

call-seq:

mtch.length -> integer
mtch.size   -> integer

Returns the number of elements in the match array.

m = /(.)(.)(\d+)(\d)/.match("THX1138.")

m.length    #=> 5


3915
3916
3917
# File 'lib/source/ruby.rb', line 3915

def length
  `this._captures.length`
end

#offset(n) ⇒ Object

FIX: Incomplete



3924
3925
# File 'lib/source/ruby.rb', line 3924

def offset(n)
end

#post_matchObject

call-seq:

mtch.post_match -> string

Returns the portion of the original string after the current match.

m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")

m.post_match    #=> ": The Movie"


3936
3937
3938
# File 'lib/source/ruby.rb', line 3936

def post_match
  `this._post`
end

#pre_matchObject

call-seq:

mtch.pre_match -> string

Returns the portion of the original string before the current match.

m = /(.)(.)(\d+)(\d)/.match("THX1138.")

m.pre_match   #=> "T"


3949
3950
3951
# File 'lib/source/ruby.rb', line 3949

def pre_match
  `this._pre`
end

#selectObject

FIX: Incomplete



3954
3955
# File 'lib/source/ruby.rb', line 3954

def select
end

#sizeObject

call-seq:

mtch.length -> integer
mtch.size   -> integer

Returns the number of elements in the match array.

m = /(.)(.)(\d+)(\d)/.match("THX1138.")

m.size    #=> 5


3967
3968
3969
# File 'lib/source/ruby.rb', line 3967

def size
  `this._captures.length`
end

#stringObject

call-seq:

mtch.string -> string

Returns a copy of the string that was matched against a pattern to produce mtch.

m1 = /(.)(.)(\d+)(\d)/.match("THX1138.")
m2 = "THX1138.".match(/(.)(.)(\d+)(\d)/)

m1.string   #=> "THX1138."
m2.string   #=> "THX1138."


3983
3984
3985
# File 'lib/source/ruby.rb', line 3983

def string
  `$q(this._string)`
end

#to_aObject

call-seq:

mtch.to_a -> array

Returns the MatchData’s internal array.

m = /(.)(.)(\d+)(\d)/.match("THX1138.")

m.to_a    #=> ["HX1138", "H", "X", "113", "8"]


3996
3997
3998
# File 'lib/source/ruby.rb', line 3996

def to_a
  `this._captures`
end

#to_sObject

call-seq:

mtch.to_s -> string

Returns the entire matched string.

m = /(.)(.)(\d+)(\d)/.match("THX1138.")

m.to_s    #=> "HX1138"


4009
4010
4011
# File 'lib/source/ruby.rb', line 4009

def to_s
  `this._captures[0]`
end

#values_atObject

FIX: Incomplete



4014
4015
# File 'lib/source/ruby.rb', line 4014

def values_at
end