Object
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"
# File lib/oniguruma.rb, line 359 359: def [](*idx) 360: if idx[0].is_a?(Symbol) 361: k = to_index( idx[0] ) 362: k && old_aref(k) 363: else 364: old_aref(*idx) 365: end 366: end
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
# File lib/oniguruma.rb, line 395 395: def begin(*idx) 396: if idx[0].is_a?(Symbol) 397: k = to_index( idx[0] ) 398: k && old_begin(k) 399: elsif idx.empty? 400: old_begin( 0 ) 401: else 402: old_begin(*idx) 403: end 404: end
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
# File lib/oniguruma.rb, line 431 431: def end(*idx) 432: if idx[0].is_a?(Symbol) 433: k = to_index( idx[0] ) 434: k && old_end(k) 435: elsif idx.empty? 436: old_end( 0 ) 437: else 438: old_end(*idx) 439: end 440: end
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]
# File lib/oniguruma.rb, line 469 469: def offset(*idx) 470: if idx[0].is_a?(Symbol) 471: k = to_index( idx[0] ) 472: k && old_offset(k) 473: elsif idx.empty? 474: old_offset( 0 ) 475: else 476: old_offset(*idx) 477: end 478: end
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
# File lib/oniguruma.rb, line 327 327: def to_index symbol 328: @named_captures && @named_captures[symbol] 329: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.