Ruby 2.6.0 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > 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) -> Regexp | nil

obj を to_regexp メソッドで Regexp オブジェクトに変換しようと試みます。

union(*pattern) -> Regexp

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

インスタンスメソッド

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

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

self === string -> bool

文字列 string との正規表現マッチを行います。マッチした場合は真を返します。

self =~ string -> Integer | nil

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

casefold? -> bool

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

encoding -> Encoding

正規表現オブジェクトのエンコーディングを表す Encoding オブジェクトを返します。

fixed_encoding? -> bool

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

hash -> Integer

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

inspect -> String

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

match(str, pos = 0) -> MatchData | nil
match(str, pos = 0) {|m| ... } -> object | nil

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

match?(str, pos = 0) -> bool

指定された文字列 str に対して 位置 pos から自身が表す正規表現によるマッチングを行います。マッチした場合 true を返し、マッチしない場合には false を返します。また、$~ などパターンマッチに関する組み込み変数の値は変更されません。

named_captures -> { String => [Integer] }

正規表現に含まれる名前付きキャプチャ(named capture)の情報を Hash で返します。

names -> [String]

正規表現に含まれる名前付きキャプチャ(named capture)の名前を文字列の配列で返します。

options -> Integer

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

source -> String

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

to_s -> String

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

~ self -> Integer | nil

変数 $_ の値との間でのマッチをとります。

定数

定義 説明
EXTENDED -> Integer

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

FIXEDENCODING -> Integer

正規表現が特定のエンコーディングの文字列にしかマッチしないことを意味します。

IGNORECASE -> Integer

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

MULTILINE -> Integer

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

NOENCODING -> Integer

正規表現のマッチ時に文字列のエンコーディングを無視し、バイト列としてマッチすることを意味します。

継承したメソッド

! != __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 equal? extend freeze frozen? 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 then to_a to_ary to_hash to_int to_io to_proc to_regexp 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

追加されるメソッド

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

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

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

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

json/add/regexp