Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

MatchData#pre_match

mtch.pre_match → str

Returns the portion of the original string before the current match. Equivalent to the special variable $`.

Example
m = /(.)(.)(\d+)(\d)/.match("THX1138.")
m.pre_match   #=> "T"
See also #post_match #regexp #offset #size also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1