class Regexp

[edit]

要約

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


/^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"

Ruby 3.0.0 から正規表現リテラルは freeze されるようになりました。


p /abc/.frozen?
# => true
p /a#{42}bc/.frozen?
# => true
p Regexp.new('abc').frozen?
# => false

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

目次

特異メソッド
インスタンスメソッド
定数

特異メソッド

compile(string, option = nil, code = nil) -> Regexp[permalink][rdoc][edit]
new(string, option = nil, code = nil) -> Regexp

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

第一引数が正規表現であれば第一引数を複製して返します。第二、第三引数は警告の上無視されます。

[PARAM] string:
正規表現を文字列として与えます。
[PARAM] option:
Regexp::IGNORECASE, Regexp::MULTILINE, Regexp::EXTENDED の論理和を指定します。 Integer 以外であれば真偽値の指定として見なされ、真(nil, false 以外)であれば Regexp::IGNORECASE の指定と同じになります。
[PARAM] code:
"n", "N" が与えられた時には、生成された正規表現のエンコーディングは ASCII-8BIT になります。それ以外の指定は警告を出力します。
[EXCEPTION] RegexpError:
正規表現のコンパイルに失敗した場合発生します。


str = "This is Regexp"
t1 = Regexp.compile("this is regexp", Regexp::IGNORECASE)
t1.match(str)
p $~ # => "This is Regexp"

t2 = Regexp.compile('
this         # ここは使用されない
\ is
\ regexp     # ここも使用されない
', Regexp::EXTENDED | Regexp::IGNORECASE)
t2.match(str)
p Regexp.last_match # => "This is Regexp"

str = "ふるいけや\nかわずとびこむ\nみずのおと"
t2 = Regexp.compile("ふる.*?と", Regexp::MULTILINE)
p t2.match(str)[0]  # => "ふるいけや\nかわずと"
escape(string) -> String[permalink][rdoc][edit]
quote(string) -> String

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

[PARAM] string:
正規表現において特別な意味をもつ文字をもつ文字列を指定します。


rp = Regexp.escape("$bc^")
p rp # => "\\$bc\\^"
last_match -> MatchData[permalink][rdoc][edit]

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



/(.)(.)/ =~ "ab"
p Regexp.last_match      # => #<MatchData:0x4599e58>
p Regexp.last_match[0]   # => "ab"
p Regexp.last_match[1]   # => "a"
p Regexp.last_match[2]   # => "b"
p Regexp.last_match[3]   # => nil
last_match(nth) -> String | nil[permalink][rdoc][edit]

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



/(.)(.)/ =~ "ab"
p Regexp.last_match      # => #<MatchData:0x4599e58>
p Regexp.last_match(0)   # => "ab"
p Regexp.last_match(1)   # => "a"
p Regexp.last_match(2)   # => "b"
p Regexp.last_match(3)   # => nil

正規表現全体がマッチしなかった場合、引数なしの Regexp.last_match はnil を返すため、 last_match[1] の形式では例外 NoMethodError が発生します。対して、last_match(1) は nil を返します。



str = "This is Regexp"
/That is Regexp/ =~ str
p Regexp.last_match # => nil
begin
  p Regexp.last_match[1] # 例外が発生する
rescue 
  puts $! # => undefined method `[]' for nil:NilClass
end
p Regexp.last_match(1) # => nil
[PARAM] nth:
整数を指定します。整数 nth が 0 の場合、マッチした文字列を返します。それ以外では、nth 番目の括弧にマッチした部分文字列を返します。
try_convert(obj) -> Regexp | nil[permalink][rdoc][edit]

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

変換に成功した場合はそれを返し、失敗時には nil を返します。



Regexp.try_convert(/re/)      # => /re/
Regexp.try_convert("re")      # => nil
union(*pattern) -> Regexp[permalink][rdoc][edit]

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


p Regexp.union(/a/, /b/, /c/) # => /(?-mix:a)|(?-mix:b)|(?-mix:c)/

引数を一つだけ与える場合は、Array を与えても Regexp を生成します。つまり、以下のように書くことができます。


arr = [/a/, /b/, /c/]
p Regexp.union(arr)  # => /(?-mix:a)|(?-mix:b)|(?-mix:c)/
# 1.8.7 より前は、以下のように書く必要があった
p Regexp.union(*arr) # => /(?-mix:a)|(?-mix:b)|(?-mix:c)/

pattern は Regexp または String で与えます。 String で与えた場合、それ自身と等しい文字列にマッチするものと解釈され、エスケープされて結果の Regexp に組み込まれます。


p Regexp.union("a", "?", "b") # => /a|\?|b/
p Regexp.union(/a/, "*") # => /(?-mix:a)|\*/

引数をひとつも与えなかった場合、決してマッチしない Regexp を返します。


p Regexp.union() # => /(?!)/

結果の Regexp が対応する文字コードは引数として与えた Regexp が扱う文字コードに一致します。固定コードに対してコンパイルされている Regexp を複数与える場合、それらのコードは一致していなければなりません。異なる固定コードに対してコンパイルされている Regexp が存在する場合、 ArgumentError が発生します。


p Regexp.union(/a/e, /b/e) # => /(?-mix:a)|(?-mix:b)/e
p Regexp.union(/a/e, /b/s) # => ArgumentError

コードが固定されている Regexp とコードが固定されていない Regexp を混ぜた場合、結果の Regexp は固定されているコードに対応するものになります。


p Regexp.union(/a/e, /b/) # => /(?-mix:a)|(?-mix:b)/e
[PARAM] pattern:
| で連結したい正規表現を指定します


# オプションは合成されない
p Regexp.union(/foo/i, /bar/x, /hoge/m) # => /(?i-mx:foo)|(?x-mi:bar)|(?m-ix:hoge)/

# 文字列そのものにマッチする
rep1 = [ "foo", "bar", "hoge"] 
p Regexp.union(*rep1) # => /foo|bar|hoge/
p Regexp.union(rep1)  # => /foo|bar|hoge/

# 下記の場合オプションがつくのは最初だけ
rep2 = [ /foo/x, "bar", "hoge"] 
p Regexp.union(*rep2) # => /(?x-mi:foo)|bar|hoge/
p Regexp.union(rep2)  # => /(?x-mi:foo)|bar|hoge/

インスタンスメソッド

self == other -> bool[permalink][rdoc][edit]
eql?(other) -> bool

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

[PARAM] other:
正規表現を指定します。


p /^eee$/   == /~eee$/x   # => false
p /^eee$/   == /~eee$/i   # => false
p /^eee$/e  == /~eee$/u   # => false
p /^eee$/ == Regexp.new("^eee$") # => true
p /^eee$/.eql?(/^eee$/)          # => true
self === string -> bool[permalink][rdoc][edit]

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

string が文字列でもシンボルでもない場合には false を返します。

このメソッドは主にcase文での比較に用いられます。

[PARAM] string:
マッチ対象文字列


a = "HELLO"
case a
when /\A[a-z]*\z/; puts "Lower case"
when /\A[A-Z]*\z/; puts "Upper case"
else;              puts "Mixed case"
end
# => Upper case

/\A[a-z]*\z/ === "HELLO" # => false
/\A[A-Z]*\z/ === "HELLO" # => true

[SEE_ALSO] Enumerable#grep, Object#===

self =~ string -> Integer | nil[permalink][rdoc][edit]

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



p /foo/ =~ "foo"  # => 0
p /foo/ =~ "afoo" # => 1
p /foo/ =~ "bar"  # => nil

組み込み変数 $~ もしくは Regexp.last_match にマッチに関する情報 MatchData が設定されます。

文字列のかわりにSymbolをマッチさせることができます。

[PARAM] string:
マッチ対象文字列
[EXCEPTION] TypeError:
string が nil でも String オブジェクトでも Symbol でもない場合発生します。


p /foo/ =~ "foo"        # => 0
p Regexp.last_match(0)  # => "foo"
p /foo/ =~ "afoo"       # => 1
p $~[0]                 # => "foo"
p /foo/ =~ "bar"        # => nil

unless /foo/ === "bar"
  puts "not match " # => not match
end

str = []
begin
  /ugo/ =~ str
rescue TypeError
  printf "! %s\t%s\n", $!, $@ # => ! can't convert Array into String       r5.rb:15
end
casefold? -> bool[permalink][rdoc][edit]

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



reg = Regexp.new("foobar", Regexp::IGNORECASE)
p reg.casefold? # => true

reg = Regexp.new("hogehoge")
p reg.casefold? # => false
encoding -> Encoding[permalink][rdoc][edit]

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

[SEE_ALSO] 正規表現/エンコーディング

fixed_encoding? -> bool[permalink][rdoc][edit]

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



# -*- coding:utf-8 -*-

r = /a/
r.fixed_encoding?                               # => false
r.encoding                                      # => #<Encoding:US-ASCII>
r =~ "\u{6666} a"                               # => 2
r =~ "\xa1\xa2 a".force_encoding("euc-jp")      # => 2
r =~ "abc".force_encoding("euc-jp")             # => 0

r = /a/u
r.fixed_encoding?                               # => true
r.encoding                                      # => #<Encoding:UTF-8>
r =~ "\u{6666} a"                               # => 2
begin
  r =~ "\xa1\xa2".force_encoding("euc-jp")
rescue => e
  e.class                                       # => Encoding::CompatibilityError
end
r =~ "abc".force_encoding("euc-jp")             # => 0

r = /\u{6666}/
r.fixed_encoding?                               # => true
r.encoding                                      # => #<Encoding:UTF-8>
r =~ "\u{6666} a"                               # => 0
begin
  r =~ "\xa1\xa2".force_encoding("euc-jp")
rescue => e
  e.class                                       # => Encoding::CompatibilityError
end
r =~ "abc".force_encoding("euc-jp")             # => nil
hash -> Integer[permalink][rdoc][edit]

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



p /abc/i.hash # => 4893115
p /abc/.hash  # => 4856055
inspect -> String[permalink][rdoc][edit]

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



p /^ugou.*?/i.to_s    # => "(?i-mx:^ugou.*?)"
p /^ugou.*?/i.inspect # => "/^ugou.*?/i"

[SEE_ALSO] Regexp#to_s

match(str, pos = 0) -> MatchData | nil[permalink][rdoc][edit]
match(str, pos = 0) {|m| ... } -> object | nil

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

省略可能な第二引数 pos を指定すると、マッチの開始位置を pos から行うよう制御できます(pos のデフォルト値は 0)。



p(/(.).(.)/.match("foobar", 3).captures)   # => ["b", "r"]
p(/(.).(.)/.match("foobar", -3).captures)  # => ["b", "r"]

pos を指定しても MatchData#offset 等の結果には影響しません。つまり、


re.match(str[pos..-1])


re.match(str, pos)

は異なります。

ブロックを渡すと、マッチした場合に限り MatchData オブジェクトがブロック引数に渡されて実行されます。マッチした場合はブロックの値を返し、マッチしなかった場合は nil を返します。



results = []
/((.)\2)/.match("foo") {|m| results << m[0] } # => ["oo"] 
/((.)\2)/.match("bar") {|m| results << m[0] } # => nil
results # => ["oo"]
[PARAM] str:
文字列を指定します。str との正規表現マッチを行います。
[PARAM] pos:
整数を指定します。マッチの開始位置を pos から行うよう制御できます(pos のデフォルト値は 0)。


reg = Regexp.new("foo")

if reg.match("foobar")
  puts "match"
end
# => match

p reg.match("foobar") # => #<MatchData:0x29403fc>
p reg.match("bar")    # => nil

p /(foo)(bar)(baz)/.match("foobarbaz").to_a.values_at(1,2,3) # => ["foo", "bar", "baz"]

便利な使いかた

正規表現にマッチした部分文字列だけが必要な場合に、


bar = /foo(.*)baz/.match("foobarbaz").to_a[1]

foo, bar, baz = /(foo)(bar)(baz)/.match("foobarbaz").to_a.values_at(1,2,3)

のように使用できます。(to_a は、マッチに失敗した場合を考慮しています。)

多重代入の規則では右辺が配列でない一つのオブジェクトで to_a メソッドを持つ場合、右辺に * を付けることで to_a の結果を利用できます。つまり、上記は以下のように書くことができます。(ここでの `_' は、$& を捨てるために適当に選んだ変数名)



_, foo, bar, baz = */(foo)(bar)(baz)/.match("foobarbaz")
p [foo, bar, baz]

# => ["foo", "bar", "baz"]

このような用途に MatchData#captures が使えると考えるかも知れませんが、captures では、マッチに失敗した場合、 nil.captures を呼び出そうとして例外 NoMethodError が発生してしまいます。



foo, bar, baz = /(foo)(bar)(baz)/.match("foobar").captures

# => -:1: undefined method `captures' for nil:NilClass (NoMethodError)

[SEE_ALSO] Regexp#match?

match?(str, pos = 0) -> bool[permalink][rdoc][edit]

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



/R.../.match?("Ruby")    # => true
/R.../.match?("Ruby", 1) # => false
/P.../.match?("Ruby")    # => false
$&                       # => nil

[SEE_ALSO] Regexp#match

named_captures -> { String => [Integer] }[permalink][rdoc][edit]

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

Hash のキーは名前付きキャプチャの名前で、値はその名前に関連付けられたキャプチャの index のリストを返します。



/(?<foo>.)(?<bar>.)/.named_captures
# => {"foo"=>[1], "bar"=>[2]}

/(?<foo>.)(?<foo>.)/.named_captures
# => {"foo"=>[1, 2]}

# 名前付きキャプチャを持たないときは空の Hash を返します。
/(.)(.)/.named_captures
# => {}
names -> [String][permalink][rdoc][edit]

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



/(?<foo>.)(?<bar>.)(?<baz>.)/.names

# => ["foo", "bar", "baz"]
   /(?<foo>.)(?<foo>.)/.names
# => ["foo"]

/(.)(.)/.names
# => []
options -> Integer[permalink][rdoc][edit]

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

これで得られるオプションには生成時に指定したもの以外のオプションを含んでいる場合があります。これらのビットは内部的に用いられているもので、Regexp.new にこれらを渡しても無視されます。



p Regexp::IGNORECASE # => 1
p //i.options        # => 1

p Regexp.new("foo", Regexp::IGNORECASE ).options # => 1
p Regexp.new("foo", Regexp::EXTENDED).options    # => 2
p Regexp.new("foo", Regexp::IGNORECASE | Regexp::EXTENDED).options # => 3
p Regexp.new("foo", Regexp::MULTILINE).options # => 4
p Regexp.new("foo", Regexp::IGNORECASE | Regexp::MULTILINE ).options # => 5
p Regexp.new("foo", Regexp::MULTILINE | Regexp::EXTENDED).options # =>6
p Regexp.new("foo", Regexp::IGNORECASE | Regexp::MULTILINE | Regexp::EXTENDED).options # => 7
source -> String[permalink][rdoc][edit]

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



re = /foo|bar|baz/i
p re.source     # => "foo|bar|baz"
to_s -> String[permalink][rdoc][edit]

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


re = /foo|bar|baz/i
p re.to_s       # => "(?i-mx:foo|bar|baz)"
p /#{re}+/o     # => /(?i-mx:foo|bar|baz)+/

ただし、後方参照を含む正規表現は意図通りにはならない場合があります。この場合は名前付きキャプチャを使用すると影響を受けにくくなります。


re = /(foo|bar)\1/      # \1 は、foo か bar
p /(baz)#{re}/          # \1 は、baz

# => /(baz)(?-mix:(foo|bar)\1)/
使用例

re = /foo|bar|baz/i
p re.to_s       # => "(?i-mx:foo|bar|baz)"
p /#{re}+/o     # => /(?i-mx:foo|bar|baz)+/

[SEE_ALSO] Regexp#inspect

~ self -> Integer | nil[permalink][rdoc][edit]

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

ちょうど以下と同じ意味です。


self =~ $_


$_ = "hogehoge"

if  /foo/
  puts "match"
else
  puts "no match"
end
# => no match
# ただし、警告がでる。warning: regex literal in condition

reg = Regexp.compile("foo")

if ~ reg
  puts "match" 
else
  puts "no match" 
end
# => no match

if reg
  puts "match" 
else
  puts "no match"
end
# => match
# reg は nil でも false でも無いので常にtrue

定数

EXTENDED -> Integer[permalink][rdoc][edit]

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

FIXEDENCODING -> Integer[permalink][rdoc][edit]

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

[SEE_ALSO] Regexp#fixed_encoding?

IGNORECASE -> Integer[permalink][rdoc][edit]

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

MULTILINE -> Integer[permalink][rdoc][edit]

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

NOENCODING -> Integer[permalink][rdoc][edit]

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

正規表現リテラルの n オプションに対応します。