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

class Regexp

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

要約

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

/^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) -> String
quote(string) -> String

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

last_match -> MatchData

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

last_match(nth) -> String | nil

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

try_convert(obj) -> re | nil

Try to convert obj into a Regexp, using to_regexp method. Returns converted regexp or nil if obj cannot be converted for any reason.

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

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

encoding -> Encoding

Returns the Encoding object that represents the encoding of obj.

fixed_encoding? -> bool

正規表現が任意の ASCII 互換エンコーディングとマッチ可能な時に false を返します。

hash -> Fixnum

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

inspect -> String

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

match(str, pos = 0) -> MatchData | nil

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

options -> Integer

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

source -> String

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

to_s -> String

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

~ -> Fixnum | nil

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

定数

定義 説明
EXTENDED -> Fixnum

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

FIXEDENCODING

@see Regexp#fixed_encoding?

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 must_be must_be_close_to must_be_empty must_be_instance_of must_be_kind_of must_be_nil must_be_same_as must_be_within_epsilon must_equal must_include must_match must_raise must_respond_to must_send must_throw 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? respond_to_missing? singleton_class 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 trust untaint untrust untrusted? wont_be wont_be_close_to wont_be_empty wont_be_instance_of wont_be_kind_of wont_be_nil wont_be_same_as wont_be_within_epsilon wont_equal wont_include wont_match wont_respond_to

追加されるメソッド

定義 説明 ライブラリ
json_create(hash) -> Regexp

JSON のオブジェクトから Ruby のオブジェクトを生成して返します。

json/add/core
to_json(*args) -> String

自身を JSON 形式の文字列に変換して返します。

json/add/core