Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
StringScanner#exist?
(::Regexp) -> ::Integer?
exist?(pattern) → byte_offset or nil
ext/strscan/strscan.c
View on GitHub ↗
static VALUE
strscan_exist_p(VALUE self, VALUE re)
{
return strscan_do_scan(self, re, 0, 0, 0);
}
Attempts to match the given pattern anywhere (at any position) n the target substring; does not modify the positions.
If the match succeeds:
-
Returns a byte offset: the distance in bytes between the current position and the end of the matched substring.
-
Sets all match values.
Example
scanner = StringScanner.new('foobarbazbatbam') scanner.pos = 6 scanner.exist?(/bat/) # => 6 put_match_values(scanner) # Basic match values: # matched?: true # matched_size: 3 # pre_match: "foobarbaz" # matched : "bat" # post_match: "bam" # Captured match values: # size: 1 # captures: [] # named_captures: {} # values_at: ["bat", nil] # []: # [0]: "bat" # [1]: nil put_situation(scanner) # Situation: # pos: 6 # charpos: 6 # rest: "bazbatbam" # rest_size: 9
If the match fails:
-
Returns
nil. -
Clears all match values.
Example
scanner.exist?(/nope/) # => nil match_values_cleared?(scanner) # => true