Skip to content
instance method

StringScanner#exist?

(::Regexp) -> ::Integer?
exist?(pattern) → byte_offset or nil

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:

Example
scanner.exist?(/nope/)         # => nil
match_values_cleared?(scanner) # => true
See also #eos? #fixed_anchor? #concat #get_byte also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0