Skip to content
instance method

StringScanner#scan_until

(::Regexp) -> ::String?
scan_until(p1)

call-seq: scan_until(pattern) -> substring or nil

Attempts to match the given pattern anywhere (at any position) in the target substring.

If the match attempt succeeds:

Example
scanner = StringScanner.new(HIRAGANA_TEXT)
scanner.string           # => "こんにちは"
scanner.pos = 6
scanner.scan_until(/ち/) # => "にち"
put_match_values(scanner)
# Basic match values:
#   matched?:       true
#   matched_size:   3
#   pre_match:      "こんに"
#   matched  :      "ち"
#   post_match:     "は"
# Captured match values:
#   size:           1
#   captures:       []
#   named_captures: {}
#   values_at:      ["ち", nil]
#   []:
#     [0]:          "ち"
#     [1]:          nil
put_situation(scanner)
# Situation:
#   pos:       12
#   charpos:   4
#   rest:      "は"
#   rest_size: 3

If the match attempt fails:

  • Clears match data.

  • Returns nil.

  • Does not update positions.

Example
scanner.scan_until(/nope/)     # => nil
match_values_cleared?(scanner) # => true
See also #scan_integer #size #scan_byte #skip also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0