Class: MatchData

Inherits:
Object
  • Object
show all
Defined in:
lib/oniguruma.rb

Instance Method Summary collapse

Instance Method Details

#[](*idx) ⇒ Object

call-seq:

mtch[i]               => obj
mtch[start, length]   => array
mtch[range]           => array
mtch[symbol]          => obj

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"


359
360
361
362
363
364
365
366
# File 'lib/oniguruma.rb', line 359

def [](*idx)
   if idx[0].is_a?(Symbol) 
      k = to_index( idx[0] )
      k && old_aref(k)
    else
      old_aref(*idx)
    end
end

#begin(*idx) ⇒ Object

call-seq:

mtch.begin(n)        => integer
mtch.begin           => integer
mtch.begin(symbol)   => integer

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


395
396
397
398
399
400
401
402
403
404
# File 'lib/oniguruma.rb', line 395

def begin(*idx)
   if idx[0].is_a?(Symbol) 
      k = to_index( idx[0] )
      k && old_begin(k)
   elsif idx.empty?
      old_begin( 0 )
   else
      old_begin(*idx)
   end
end

#end(*idx) ⇒ Object

call-seq:

mtch.end(n)   => integer

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


431
432
433
434
435
436
437
438
439
440
# File 'lib/oniguruma.rb', line 431

def end(*idx)
   if idx[0].is_a?(Symbol) 
      k = to_index( idx[0] )
      k && old_end(k)
   elsif idx.empty?
      old_end( 0 )
   else
      old_end(*idx)
   end
end

#offset(*idx) ⇒ Object

call-seq:

mtch.offset(n)      => array
mtch.offset         => array
mtch.offset(symbol) => array

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]


469
470
471
472
473
474
475
476
477
478
# File 'lib/oniguruma.rb', line 469

def offset(*idx)
   if idx[0].is_a?(Symbol) 
      k = to_index( idx[0] )
      k && old_offset(k)
   elsif idx.empty?
      old_offset( 0 )
   else
      old_offset(*idx)
   end
end

#old_arefObject



331
# File 'lib/oniguruma.rb', line 331

alias old_aref :[]

#old_beginObject



368
# File 'lib/oniguruma.rb', line 368

alias old_begin :begin

#old_endObject



406
# File 'lib/oniguruma.rb', line 406

alias old_end :end

#old_offsetObject



442
# File 'lib/oniguruma.rb', line 442

alias old_offset :offset

#to_index(symbol) ⇒ Object

call-seq:

to_index[symbol]      => int or nil

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


327
328
329
# File 'lib/oniguruma.rb', line 327

def to_index symbol
   @named_captures && @named_captures[symbol]
end