Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

StringScanner#named_captures

scanner.named_captures → hash

Returns a hash of string variables matching the regular expression.

Example
scan = StringScanner.new('foobarbaz')
scan.match?(/(?<f>foo)(?<r>bar)(?<z>baz)/)
scan.named_captures # -> {"f"=>"foo", "r"=>"bar", "z"=>"baz"}
See also #matched_size #peek #matched? #peep also in 4.1 · 4.0 · 3.4 · 3.2