Ruby 1.8.7 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Regexpクラス

class Regexp

クラスの継承リスト: Regexp < Object < Kernel

要約

正規表現のクラス。正規表現のリテラルはスラッシュで囲んだ形式 で記述します。

/^this is regexp/

Regexp.new(string) を使って正規表現オブジェクトを動的に生成する こともできます。

str = "this is regexp"
rp1 = Regexp.new("^this is regexp")
p rp1 =~ str           #=> 0
p Regexp.last_match[0] #=> "this is regexp"

正規表現リテラル/正規表現リテラル も参照してください。

特異メソッド

定義 説明
compile(string, option = nil, code = nil) -> Regexp
new(string, option = nil, code = nil) -> Regexp

文字列 string をコンパイルして正規表現オブジェクトを生成して返します。

escape(string, kcode = $KCODE) -> String
quote(string, kcode = $KCODE) -> String

string の中で正規表現において特別な意味を持つ文字の直前にエ スケープ文字(バックスラッシュ)を挿入した文字列を返します。

last_match -> MatchData

カレントスコープで最後に行った正規表現マッチの MatchData オ ブジェクトを返します。このメソッドの呼び出しは $~ の参照と同じです。

last_match(nth) -> String | nil

整数 nth が 0 の場合、マッチした文字列を返します ($&)。それ以外では、nth 番目の括弧にマッチ した部分文字列を返します($1,$2,...)。 対応する括弧がない場合やマッチしなかった場合には nil を返し ます。

union(*pattern) -> Regexp

引数として与えた pattern を選択 | で連結し、Regexp として返します。 結果の Regexp は与えた pattern のどれかにマッチする場合にマッチするものになります。

インスタンスメソッド

定義 説明
self == other -> bool
eql?(other) -> bool

otherが同じパターン、オプション、文字コードの正規表現であったらtrueを返します。

self =~ string -> Fixnum | nil
self === string -> bool

文字列 string との正規表現マッチを行います。マッチした場合、 マッチした位置のインデックスを返します(先頭は0)。マッチしなかった 場合、あるいは string が nil の場合には nil を返 します。

casefold? -> bool

正規表現が大文字小文字の判定をしないようにコンパイルされている時、 真を返します。

hash -> Fixnum

正規表現のオプションやテキストに基づいたハッシュ値を返します。

inspect -> String

Regexp#to_s より自然な文字列を返します。

kcode -> String | nil

その正規表現が対応するようにコンパイルされている文字コードを $KCODE と同じ形式で返します。もし、正規表現が固定 コードに対してコンパイルされていない(マッチ時点での $KCODE の値を用いる)場合には、nil を返します。

match(str) -> MatchData | nil

指定された文字列 str に対して 自身が表す正規表現によるマッチングを行います。マッチした場合には結果を MatchData オブジェクトで返します。 マッチしなかった場合 nil を返します。

options -> Integer

正規表現の生成時に指定されたオプションを返します。戻り値は、 Regexp::EXTENDED, Regexp::IGNORECASE, Regexp::MULTILINE の論理和です。

source -> String

その正規表現のもととなった文字列表現を生成して返します。

to_s -> String

正規表現の文字列表現を生成して返します。返される文字列は他の正規表 現に埋め込んでもその意味が保持されるようになっています。

~ -> Fixnum | nil

変数 $_ の値との間でのマッチをとります。ちょうど以下と同じ意 味です。

定数

定義 説明
EXTENDED -> Fixnum

バックスラッシュでエスケープされていない空白と # から改行までを無 視します。正規表現リテラルの //x オプションと同じ です。(空白を入れる場合は\でエスケープして\ (<-空白)と 指定します)

IGNORECASE -> Fixnum

文字の大小の違いを無視します。 正規表現リテラルの //i オプションと同じです。

MULTILINE -> Fixnum

複数行モード。正規表現 "." が改行にマッチするようになります。 正規表現リテラルの //m オプションと同じです。

継承したメソッド

__id__ __send__ _dump class clone dclone display enum_for equal? extend freeze frozen? initialize initialize_copy instance_eval instance_exec instance_of? instance_variable_defined? instance_variable_get instance_variable_set instance_variables is_a? marshal_dump marshal_load method method_missing methods nil? pretty_inspect pretty_print pretty_print_cycle pretty_print_inspect pretty_print_instance_variables private_methods protected_methods public_methods remove_instance_variable respond_to? singleton_method_added singleton_method_removed singleton_method_undefined singleton_methods taint tainted? tap to_a to_ary to_hash to_int to_io to_proc to_regexp to_str to_yaml to_yaml_properties to_yaml_style untaint

追加されるメソッド

定義 説明 ライブラリ
self & other -> RedAnd

RegAnd.new(self, other) を返します。

eregex
self | other -> RegOr

RegOr.new(self, other) を返します。

eregex