Skip to content
instance method

StringScanner#check

(::Regexp) -> ::String?
check(pattern) → matched_substring or nil

Attempts to match the given pattern at the beginning of the target substring; does not modify the positions.

If the match succeeds:

Example
scanner = StringScanner.new('foobarbaz')
scanner.pos = 3
scanner.check('bar') # => "bar"
put_match_values(scanner)
# Basic match values:
#   matched?:       true
#   matched_size:   3
#   pre_match:      "foo"
#   matched  :      "bar"
#   post_match:     "baz"
# Captured match values:
#   size:           1
#   captures:       []
#   named_captures: {}
#   values_at:      ["bar", nil]
#   []:
#     [0]:          "bar"
#     [1]:          nil
# => 0..1
put_situation(scanner)
# Situation:
#   pos:       3
#   charpos:   3
#   rest:      "barbaz"
#   rest_size: 6

If the match fails:

Example
scanner.check(/nope/)          # => nil
match_values_cleared?(scanner) # => true
See also #charpos #check_until #captures #concat also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0