Skip to content

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

instance method

StringScanner#captures

captures

Returns the subgroups in the most recent match (not including the full match). If nothing was priorly matched, it returns nil.

Example
s = StringScanner.new("Fri Dec 12 1975 14:39")
s.scan(/(\w+) (\w+) (\d+) /)       # -> "Fri Dec 12 "
s.captures                         # -> ["Fri", "Dec", "12"]
s.scan(/(\w+) (\w+) (\d+) /)       # -> nil
s.captures                         # -> nil
See also #beginning_of_line? #charpos #[] #check also in 4.1 · 4.0 · 3.4 · 3.2 · 3.1 · 3.0