Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

StringScanner#matched

() -> ::String?
matched → matched_substring or nil

Returns the matched substring from the most recent match attempt if it was successful, or nil otherwise; see Basic Matched Values:

Example
scanner = StringScanner.new('foobarbaz')
scanner.matched        # => nil
scanner.pos = 3
scanner.match?(/bar/)  # => 3
scanner.matched        # => "bar"
scanner.match?(/nope/) # => nil
scanner.matched        # => nil
See also #matched? #match? #integer_at #matched_size also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0