Ruby 2.4.0 リファレンスマニュアル > ライブラリ一覧 > strscanライブラリ > StringScannerクラス

class StringScanner

クラス・モジュールの継承リスト: StringScanner < Object < Kernel < BasicObject

要約

StringScanner は文字列スキャナクラスです。簡単に高速なスキャナを記述できます。



require 'strscan'

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

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

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

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

StringScanner オブジェクトはスキャンする文字列と「スキャンポインタ」のセットです。スキャンポインタとはスキャンしおわったところを示すインデックスのことです。オブジェクト作成直後にはスキャンポインタは文字列先頭にあり、その地点でのみマッチを試します。マッチしたらその後ろにポインタを進めます。



require 'strscan'

## a string and a scan pointer   ("_" = scan pointer)

s = StringScanner.new('This is an example string')
# _This is an example string     s.eos? = false
s.scan(/\w+/)
# This_ is an example string     s.eos? = false
s.scan(/\s+/)
# This _is an example string     s.eos? = false
s.scan(/\w+/)
# This is_ an example string     s.eos? = false
s.scan(/\s+/)
# This is _an example string     s.eos? = false
s.scan(/\w+/)
# This is an_ example string     s.eos? = false
s.scan(/\s+/)
# This is an _example string     s.eos? = false
s.scan(/\w+/)
# This is an example_ string     s.eos? = false
s.scan(/\s+/)
# This is an example _string     s.eos? = false
s.scan(/\w+/)
# This is an example string_     s.eos? = true

現在のスキャンポインタがさす地点以外でもマッチしたい場合は、StringScanner#scan_untilなどを使ってください。

例: scan, scan_until の動作の違い

require 'strscan'

def case1
  s = StringScanner.new('test string')
  p s.scan(/t/)       #=> "t"
  p s.scan(/\w+/)     #=> "est"
  p s.scan(/string/)  #=> nil
  p s.scan(/\s+/)     #=> " "
  p s.scan(/string/)  #=> "string"
end

def case2
  s = StringScanner.new('test string')
  p s.scan_until(/t/)       #=> "t"
  p s.scan_until(/\w+/)     #=> "est"
  p s.scan_until(/string/)  #=> " string"
  p s.scan_until(/\s+/)     #=> nil
  p s.scan_until(/string/)  #=> nil
end

p "case1"
case1
p "case2"
case2

スキャンポインタの位置は文字単位でなくバイト単位となります。

例:

# 次の行以降の内容を EUC-JP として保存して試してください
# vim:set fileencoding=euc-jp:
require 'strscan'
s = StringScanner.new("るびい") # 文字コードはEUC-JPとします
p s.exist?(/び/) #=> 4

StringScanner は $~ $& $1 $2 …… などの正規表現関連変数をセットしません。代わりに StringScanner#[], StringScanner#matched? などのマッチデータ関連メソッドを使ってください。

特異メソッド

定義 説明
must_C_version -> self

このメソッドは後方互換性のために定義されています。

new(str, dup = false) -> StringScanner

新しい StringScanner オブジェクトを生成します。

インスタンスメソッド

定義 説明
self << str -> self
concat(str) -> self

操作対象の文字列に対し str を破壊的に連結します。マッチ記録は変更されません。

self[nth] -> String | nil

前回マッチした正規表現の nth 番目のかっこに対応する部分文字列を返します。インデックス 0 はマッチした部分全体です。前回のマッチが失敗していると常に nil を返します。

beginning_of_line? -> bool
bol? -> bool

スキャンポインタが行頭を指しているなら true を、行頭以外を指しているなら false を返します。

charpos -> Integer

現在のスキャンポインタのインデックスを文字単位で返します。

check(regexp) -> String | nil

現在位置から regexp とのマッチを試みます。マッチに成功したらマッチした部分文字列を返します。マッチに失敗したら nil を返します。

check_until(regexp) -> String | nil

regexp が一致するまで文字列をスキャンします。マッチに成功したらスキャン開始位置からマッチ部分の末尾までの部分文字列を返します。マッチに失敗したら nil を返します。

terminate -> self
clear -> self

スキャンポインタを文字列末尾後まで進め、マッチ記録を捨てます。

eos? -> bool
empty? -> bool

スキャンポインタが文字列の末尾を指しているなら true を、末尾以外を指しているなら false を返します。

exist?(regexp) -> Integer | nil

スキャンポインタの位置から,次にマッチする文字列の末尾までの長さを返します。

get_byte -> String | nil
getbyte -> String | nil

1 バイトスキャンして文字列で返します。スキャンポインタをその後ろに進めます。スキャンポインタが文字列の末尾を指すなら nil を返します。

getch -> String | nil

一文字スキャンして文字列で返します。スキャンポインタをその後ろに進めます。スキャンポインタが文字列の末尾を指すならnilを返します。

inspect -> String

StringScannerオブジェクトを表す文字列を返します。

match?(regexp) -> Integer | nil

スキャンポインタの地点だけで regexp と文字列のマッチを試します。マッチしたら、スキャンポインタは進めずにマッチした部分文字列の長さを返します。マッチしなかったら nil を返します。

matched -> String | nil

前回マッチした部分文字列を返します。前回のマッチに失敗していると nil を返します。

matched? -> bool

前回のマッチが成功していたら true を、失敗していたら false を返します。

matched_size -> Integer | nil

前回マッチした部分文字列の長さを返します。前回マッチに失敗していたら nil を返します。

matchedsize -> Integer | nil

StringScanner#matched_size と同じです。

peek(bytes) -> String
peep(bytes) -> String

スキャンポインタから長さ bytes バイト分だけ文字列を返します。

pointer -> Integer
pos -> Integer

現在のスキャンポインタのインデックスを返します。

pointer=(n)
pos=(n)

スキャンポインタのインデックスを n にセットします。

post_match -> String | nil

前回マッチを行った文字列のうち、マッチしたところよりも後ろの部分文字列を返します。前回のマッチが失敗していると常に nil を返します。

pre_match -> String | nil

前回マッチを行った文字列のうち、マッチしたところよりも前の部分文字列を返します。前回のマッチが失敗していると常に nil を返します。

reset -> self

スキャンポインタを文字列の先頭 (インデックス 0) に戻し、マッチ記録を捨てます。

rest -> String

文字列の残り (rest) を返します。具体的には、スキャンポインタが指す位置からの文字列を返します。

rest? -> bool

文字列が残っているならば trueを、残っていないならば false を返します。

rest_size -> Integer
restsize -> Integer

文字列の残りの長さを返します。 stringscanner.rest.size と同じです。

scan(regexp) -> String | nil

スキャンポインタの地点だけで regexp と文字列のマッチを試します。マッチしたら、スキャンポインタを進めて正規表現にマッチした部分文字列を返します。マッチしなかったら nil を返します。

scan_full(regexp, s, f) -> object

スキャンポインタの位置から regexp と文字列のマッチを試します。

scan_until(regexp) -> String | nil

regexp で指定された正規表現とマッチするまで文字列をスキャンします。マッチに成功したらスキャンポインタを進めて、スキャン開始位置からマッチ部分の末尾までの部分文字列を返します。マッチに失敗したら nil を返します。

search_full(regexp, s, f) -> object

regexp で指定された正規表現とマッチするまで文字列をスキャンします。

skip(regexp) -> Integer | nil

スキャンポインタの地点だけで regexp と文字列のマッチを試します。マッチしたらスキャンポインタを進めマッチした部分文字列の長さを返します。マッチしなかったら nil を返します。

skip_until(regexp) -> Integer | nil

regexp が一致するまで文字列をスキャンします。マッチに成功したらスキャンポインタを進めて、スキャン開始位置からマッチ部分の末尾までの部分文字列の長さを返します。マッチに失敗したら nil を返します。

string -> String

スキャン対象にしている文字列を返します。

string=(str)

スキャン対象の文字列を str に変更して、マッチ記録を捨てます。

unscan -> self

スキャンポインタを前回のマッチの前の位置に戻します。

定数

定義 説明
Id -> String

StringScanner クラスの詳しいバージョンを文字列で返します。この文字列は Object#freeze されています。

Version -> String

StringScanner クラスのバージョンを文字列で返します。この文字列は Object#freeze されています。

継承したメソッド

! != __id__ __send__ instance_eval instance_exec method_missing singleton_method_added singleton_method_removed singleton_method_undefined !~ <=> == === =~ _dump class clone define_singleton_method display enum_for eql? equal? extend freeze frozen? hash initialize initialize_copy instance_of? instance_variable_defined? instance_variable_get instance_variable_set instance_variables is_a? itself marshal_dump marshal_load method methods nil? object_id pretty_inspect pretty_print pretty_print_cycle pretty_print_inspect pretty_print_instance_variables private_methods protected_methods psych_to_yaml public_method public_methods public_send remove_instance_variable respond_to? respond_to_missing? send singleton_class singleton_method singleton_methods taint tainted? tap to_a to_ary to_hash to_int to_io to_proc to_regexp to_s to_str trust untaint untrust untrusted? .yaml_tag ::ARGF ::ARGV ::DATA ::ENV ::FALSE ::NIL ::RUBY_COPYRIGHT ::RUBY_DESCRIPTION ::RUBY_ENGINE ::RUBY_ENGINE_VERSION ::RUBY_PATCHLEVEL ::RUBY_PLATFORM ::RUBY_RELEASE_DATE ::RUBY_REVISION ::RUBY_VERSION ::SCRIPT_LINES__ ::STDERR ::STDIN ::STDOUT ::TOPLEVEL_BINDING ::TRUE