instance method
StringScanner#check
(::Regexp) -> ::String?
check(pattern) → matched_substring or nil
ext/strscan/strscan.c
View on GitHub ↗
static VALUE
strscan_check(VALUE self, VALUE re)
{
return strscan_do_scan(self, re, 0, 1, 1);
}
Attempts to match the given pattern at the beginning of the target substring; does not modify the positions.
If the match succeeds:
-
Returns the matched substring.
-
Sets all match values.
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:
-
Returns
nil. -
Clears all match values.
Example
scanner.check(/nope/) # => nil match_values_cleared?(scanner) # => true