← index

Match

inherits Object

One resolved regex match — what Regex.match: answers. s is the matched text, at: reads a capture group by index (1-based; 0 is the whole match) or by name, captures lists them all, and bind: destructures them into a block's parameters.

(#/(?<hour>\d+):(?<min>\d+)/.match:'at 9:40').at:'min'     "* -> 40

Instance methods

at:

A capture group's text: an Integer reads by position (1-based; 0 is the whole match), a String or Symbol by group name. Nil for a group that is out of range, unknown, or did not participate in the match.

(#/(\d+)-(\d+)/.match:'10-20').at:2                "* -> 20
(#/(?<a>x)|(?<b>y)/.match:'y').at:'a'              "* -> nil

native

bind:

Destructure the capture groups into a block: a parameter named after a named group gets that group's text, any other parameter binds positionally (first parameter → group 1, and so on). A group that did not participate binds nil. Answers the block's value.

(#/(\d+)-(\d+)/.match:'10-20').bind:{ |lo hi| hi.to_integer - lo.to_integer }     "* -> 10

native

captures

Every capture group's text in position order (group 0, the whole match, is s — not listed here). A group that did not participate is nil.

(#/(\d+)-(\d+)/.match:'10-20').captures     "* -> #(10 20)

native

s

The text the whole pattern matched (capture group 0).

(#/b+/.match:'abbbc').s     "* -> bbb

native