MatchData

The class handling regular expression match data. Instances of this class can be obtained via

Superclass

Methods

self[n]

Returns the nth substring. 0 signifies the entire matching string. When the value of n is negative, it is treated as a backwards index (the final element being in the position -1). When the nth element does not exist, returns nil.

/(foo)(bar)(BAZ)?/ =~ "foobarbaz"
p $~.to_a       # => ["foobar", "foo", "bar", nil]
p $~[0]         # => "foobar"
p $~[1]         # => "foo"
p $~[2]         # => "bar"
p $~[3]         # => nil        (doesn't match)
p $~[4]         # => nil        (out of range)
p $~[-2]        # => "bar"
post_match

Returns the string after the matching portion.

pre_match

Returns the string before the matching portion.

to_a

Returns an array containing $&, $1, $2, ...

to_s

Returns the entire matching string.

Converted from CHM to HTML with chm2web Pro 2.85 (unicode)