W3cubDocs

/Crystal

class StringScanner

Overview

StringScanner provides for lexical scanning operations on a String.

Example

require "string_scanner"

s = StringScanner.new("This is an example string")
s.eos? # => false

s.scan(/\w+/) # => "This"
s.scan(/\w+/) # => nil
s.scan(/\s+/) # => " "
s.scan(/\s+/) # => nil
s.scan(/\w+/) # => "is"
s.eos?        # => false

s.scan(/\s+/) # => " "
s.scan(/\w+/) # => "an"
s.scan(/\s+/) # => " "
s.scan(/\w+/) # => "example"
s.scan(/\s+/) # => " "
s.scan(/\w+/) # => "string"
s.eos?        # => true

s.scan(/\s+/) # => nil
s.scan(/\w+/) # => nil

Scanning a string means remembering the position of a scan offset, which is just an index. Scanning moves the offset forward, and matches are sought after the offset; usually immediately after it.

Method Categories

Methods that advance the scan offset:

Methods that look ahead:

Methods that deal with the position of the offset:

Methods that deal with the last match:

Miscellaneous methods:

Defined in:

string_scanner.cr

Constructors

Instance Method Summary

  • #[](n)

    Returns the n-th subgroup in the most recent match.

  • #[]?(n)

    Returns the nilable n-th subgroup in the most recent match.

  • #check(pattern)

    Returns the value that #scan would return, without advancing the scan offset.

  • #check_until(pattern)

    Returns the value that #scan_until would return, without advancing the scan offset.

  • #eos?

    Returns true if the scan offset is at the end of the string.

  • #inspect(io : IO) : Nil

    Writes a representation of the scanner.

  • #offset

    Returns the current position of the scan offset.

  • #offset=(position : Int)

    Sets the position of the scan offset.

  • #peek(len)

    Extracts a string corresponding to string[offset,len], without advancing the scan offset.

  • #reset

    Resets the scan offset to the beginning and clears the last match.

  • #rest

    Returns the remainder of the string after the scan offset.

  • #scan(pattern)

    Tries to match with pattern at the current position.

  • #scan_until(pattern)

    Scans the string until the pattern is matched.

  • #skip(pattern)

    Attempts to skip over the given pattern beginning with the scan offset.

  • #skip_until(pattern)

    Attempts to skip until the given pattern is found after the scan offset.

  • #string : String

    Returns the string being scanned.

  • #terminate

    Moves the scan offset to the end of the string and clears the last match.

Constructor Detail

def self.new(str : String)Source

Instance Method Detail

def [](n)Source

Returns the n-th subgroup in the most recent match.

Raises an exception if there was no last match or if there is no subgroup.

require "string_scanner"

s = StringScanner.new("Fri Dec 12 1975 14:39")
regex = /(?<wday>\w+) (?<month>\w+) (?<day>\d+)/
s.scan(regex) # => "Fri Dec 12"
s[0]          # => "Fri Dec 12"
s[1]          # => "Fri"
s[2]          # => "Dec"
s[3]          # => "12"
s["wday"]     # => "Fri"
s["month"]    # => "Dec"
s["day"]      # => "12"

def []?(n)Source

Returns the nilable n-th subgroup in the most recent match.

Returns nil if there was no last match or if there is no subgroup.

require "string_scanner"

s = StringScanner.new("Fri Dec 12 1975 14:39")
regex = /(?<wday>\w+) (?<month>\w+) (?<day>\d+)/
s.scan(regex)  # => "Fri Dec 12"
s[0]?          # => "Fri Dec 12"
s[1]?          # => "Fri"
s[2]?          # => "Dec"
s[3]?          # => "12"
s[4]?          # => nil
s["wday"]?     # => "Fri"
s["month"]?    # => "Dec"
s["day"]?      # => "12"
s["year"]?     # => nil
s.scan(/more/) # => nil
s[0]?          # => nil

def check(pattern)Source

Returns the value that #scan would return, without advancing the scan offset. The last match is still saved, however.

require "string_scanner"

s = StringScanner.new("this is a string")
s.offset = 5
s.check(/\w+/) # => "is"
s.check(/\w+/) # => "is"

def check_until(pattern)Source

Returns the value that #scan_until would return, without advancing the scan offset. The last match is still saved, however.

require "string_scanner"

s = StringScanner.new("test string")
s.check_until(/tr/) # => "test str"
s.check_until(/g/)  # => "test string"

def eos?Source

Returns true if the scan offset is at the end of the string.

require "string_scanner"

s = StringScanner.new("this is a string")
s.eos?                # => false
s.scan(/(\w+\s?){4}/) # => "this is a string"
s.eos?                # => true

def inspect(io : IO) : NilSource

Writes a representation of the scanner.

Includes the current position of the offset, the total size of the string, and five characters near the current position.

def offsetSource

Returns the current position of the scan offset.

def offset=(position : Int)Source

Sets the position of the scan offset.

def peek(len)Source

Extracts a string corresponding to string[offset,len], without advancing the scan offset.

def resetSource

Resets the scan offset to the beginning and clears the last match.

def restSource

Returns the remainder of the string after the scan offset.

require "string_scanner"

s = StringScanner.new("this is a string")
s.scan(/(\w+\s?){2}/) # => "this is "
s.rest                # => "a string"

def scan(pattern)Source

Tries to match with pattern at the current position. If there's a match, the scanner advances the scan offset, the last match is saved, and it returns the matched string. Otherwise, the scanner returns nil.

require "string_scanner"

s = StringScanner.new("test string")
s.scan(/\w+/)   # => "test"
s.scan(/\w+/)   # => nil
s.scan(/\s\w+/) # => " string"
s.scan(/.*/)    # => ""

def scan_until(pattern)Source

Scans the string until the pattern is matched. Returns the substring up to and including the end of the match, the last match is saved, and advances the scan offset. Returns nil if no match.

require "string_scanner"

s = StringScanner.new("test string")
s.scan_until(/tr/) # => "test str"
s.scan_until(/tr/) # => nil
s.scan_until(/g/)  # => "ing"

def skip(pattern)Source

Attempts to skip over the given pattern beginning with the scan offset. In other words, the pattern is not anchored to the current scan offset.

If there's a match, the scanner advances the scan offset, the last match is saved, and it returns the size of the skipped match. Otherwise it returns nil and does not advance the offset.

This method is the same as #scan, but without returning the matched string.

def skip_until(pattern)Source

Attempts to skip until the given pattern is found after the scan offset. In other words, the pattern is not anchored to the current scan offset.

If there's a match, the scanner advances the scan offset, the last match is saved, and it returns the size of the skip. Otherwise it returns nil and does not advance the offset.

This method is the same as #scan_until, but without returning the matched string.

def string : StringSource

Returns the string being scanned.

def terminateSource

Moves the scan offset to the end of the string and clears the last match.

© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/StringScanner.html