るりまサーチ (Ruby 2.2.0)

最速Rubyリファレンスマニュアル検索!
9件ヒット [1-9件を表示] (0.082秒)
トップページ > バージョン:2.2.0[x] > 種類:特異メソッド[x] > クラス:Regexp[x]

ライブラリ

キーワード

検索結果

Regexp.compile(string, option = nil, code = nil) -> Regexp (7.0)

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

...@param option Regexp::IGNORECASE, Regexp::MULTILINE,
Regexp
::EXTENDED
の論理和を指定します。
Integer 以外であれば真偽値の指定として見なされ
、真(nil, false 以外)であれば
Regexp
::IGNORECASE...
...を出力します。

@raise RegexpError 正規表現のコンパイルに失敗した場合発生します。

//emlist[例][ruby]{
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)...

Regexp.escape(string) -> String (7.0)

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

...持つ文字の直前にエ
スケープ文字(バックスラッシュ)を挿入した文字列を返します。

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

//emlist[例][ruby]{
rp = Regexp.escape("$bc^")
p rp # => "\\$bc\\^"
//}...

Regexp.json_create(hash) -> Regexp (7.0)

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

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

@param hash 適切なキーを持つハッシュを指定します。

Regexp.last_match -> MatchData (7.0)

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

...のメソッドの呼び出しは $~
の参照と同じです。

//emlist[例][ruby]{
/(.)(.)/ =~ "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(nth) -> String | nil (7.0)

整数 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 は...
...を返します。

//emlist[例][ruby]{
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 整数を指...

絞り込み条件を変える

Regexp.new(string, option = nil, code = nil) -> Regexp (7.0)

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

...@param option Regexp::IGNORECASE, Regexp::MULTILINE,
Regexp
::EXTENDED
の論理和を指定します。
Integer 以外であれば真偽値の指定として見なされ
、真(nil, false 以外)であれば
Regexp
::IGNORECASE...
...を出力します。

@raise RegexpError 正規表現のコンパイルに失敗した場合発生します。

//emlist[例][ruby]{
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)...

Regexp.quote(string) -> String (7.0)

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

...持つ文字の直前にエ
スケープ文字(バックスラッシュ)を挿入した文字列を返します。

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

//emlist[例][ruby]{
rp = Regexp.escape("$bc^")
p rp # => "\\$bc\\^"
//}...

Regexp.try_convert(obj) -> Regexp | nil (7.0)

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

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

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

//emlist[例][ruby]{
Regexp
.try_convert(/re/) # => /re/
Regexp
.try_convert("re") # => nil
//}...

Regexp.union(*pattern) -> Regexp (7.0)

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

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

//emlist[][ruby]{
p Regexp.union(/a/, /b/, /c/) # => /(?-mix:a)|(?-mix:b)|(?-mix:c)/
//}...
...ay を与えても Regexp を生成します。
つまり、以下のように書くことができます。

//emlist[][ruby]{
arr = [/a/, /b/, /c/]
p Regexp.union(arr) # => /(?-mix:a)|(?-mix:b)|(?-mix:c)/
# 1.8.7 より前は、以下のように書く必要があった
p Regexp.union(*arr) # =>...
...rn は Regexp または String で与えます。
String で与えた場合、それ自身と等しい文字列にマッチするものと解釈され、
エスケープされて結果の Regexp に組み込まれます。

//emlist[][ruby]{
p Regexp.union("a", "?", "b") # => /a|\?|b/
p Regexp.union...