るりまサーチ

最速Rubyリファレンスマニュアル検索!
930件ヒット [1-100件を表示] (0.022秒)
トップページ > クエリ:Regex[x] > クエリ:正規表現[x]

別のキーワード

  1. 正規表現
  2. 正規表現 symbol
  3. 正規表現 spec/m17n
  4. 正規表現 matchdata
  5. rubyで使われる記号の意味(正規表現の複雑な記号は除く)

ライブラリ

クラス

モジュール

キーワード

検索結果

<< 1 2 3 ... > >>

Regexp (26044.0)

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

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

//emlist[][ruby]{
/^this is regexp/
//}

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

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


spec/regexp や d:spec/literal#regexp も参照してください。...
...xp"
rp1 = Regexp.new("^this is regexp")
p rp1 =~ str # => 0
p Regexp.last_match[0] # => "this is regexp"
//}

Ruby 3.0.0 から正規表現リテラルは freeze されるようになりました。
//emlist[][ruby]{
p /abc/.frozen?
# => true
p /a#{42}bc/.frozen?
# => true
p Regexp.new('ab...
...c').frozen?
# => false
//}

spec/regexp や d:spec/literal#regexp も参照してください。...

RegexpError (26016.0)

正規表現のコンパイルに失敗したときに発生します。

...正規表現のコンパイルに失敗したときに発生します。

例:

$ ruby -e 'Regexp.compile("*")'
-e:1:in `initialize': target of repeat operator is not specified: /*/ (RegexpError)
from -e:1:in `Regexp#compile'
from -e:1...

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

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

...して正規表現オブジェクトを生成して返します。

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

@param string 正規表現を文字列として与えます。

@param option Regexp::IGNORE...
...CASE, Regexp::MULTILINE,
Regex
p::EXTENDED
の論理和を指定します。
Integer 以外であれば真偽値の指定として見なされ
、真(nil, false 以外)であれば
Regex
p::IGNORECASE の指定と同じになりま...
...された正規表現のエンコーディングは ASCII-8BIT になります。
それ以外の指定は警告を出力します。

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

//emlist[例][ruby]{
str = "This is Regexp"
t1 = Regexp.compil...

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

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

...して正規表現オブジェクトを生成して返します。

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

@param string 正規表現を文字列として与えます。

@param option Regexp::IGNORE...
...CASE, Regexp::MULTILINE,
Regex
p::EXTENDED
の論理和を指定します。
Integer 以外であれば真偽値の指定として見なされ
、真(nil, false 以外)であれば
Regex
p::IGNORECASE の指定と同じになりま...
...された正規表現のエンコーディングは ASCII-8BIT になります。
それ以外の指定は警告を出力します。

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

//emlist[例][ruby]{
str = "This is Regexp"
t1 = Regexp.compil...

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

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

...正規表現オブジェクトを生成して返します。

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

@param string 正規表現を文字列として与えます。

@param option Regexp::IGNORECASE, Regexp::M...
...E,
Regex
p::EXTENDED
の論理和を指定します。
Integer 以外であれば真偽値の指定として見なされ
、真(nil, false 以外)であれば
Regex
p::IGNORECASE の指定と同じになります。

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

//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.new(string, option = nil) -> Regexp (23134.0)

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

...正規表現オブジェクトを生成して返します。

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

@param string 正規表現を文字列として与えます。

@param option Regexp::IGNORECASE, Regexp::M...
...E,
Regex
p::EXTENDED
の論理和を指定します。
Integer 以外であれば真偽値の指定として見なされ
、真(nil, false 以外)であれば
Regex
p::IGNORECASE の指定と同じになります。

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

//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.union(*pattern) -> Regexp (23106.0)

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

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

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

//emlist[][ruby]{
arr = [/a/, /b/, /c/]
p Regexp.union(arr) # => /(?-mix:a)|(?-mix:b)|(?-mix:c)/
# 1.8.7 より前は、以下のように書く必要があった
p Regexp.union(*arr) # =>...
... Regexp とコードが固定されていない Regexp を混ぜた場合、
結果の Regexp は固定されているコードに対応するものになります。

//emlist[][ruby]{
p Regexp.union(/a/e, /b/) # => /(?-mix:a)|(?-mix:b)/e
//}

@param pattern | で連結したい正規表現...

Regexp::MULTILINE -> Integer (23032.0)

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

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

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

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

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

省略可能な第二引数 pos...
...r 文字列を指定します。str との正規表現マッチを行います。

@param pos 整数を指定します。マッチの開始位置を pos から行うよう制御できます(pos のデフォルト値は 0)。

//emlist[例][ruby]{
reg = Regexp.new("foo")

if reg.match("foobar")
put...
...match("bar") # => nil

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

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

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

foo, bar, baz...

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

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

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

省略可能な第二引数 pos...
...r 文字列を指定します。str との正規表現マッチを行います。

@param pos 整数を指定します。マッチの開始位置を pos から行うよう制御できます(pos のデフォルト値は 0)。

//emlist[例][ruby]{
reg = Regexp.new("foo")

if reg.match("foobar")
put...
...match("bar") # => nil

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

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

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

foo, bar, baz...

絞り込み条件を変える

Regexp#==(other) -> bool (23022.0)

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

...ン、文字コードの正規表現であったらtrueを返します。

@param other 正規表現を指定します。

//emlist[例][ruby]{
p /^eee$/ == /~eee$/x # => false
p /^eee$/ == /~eee$/i # => false
p /^eee$/e == /~eee$/u # => false
p /^eee$/ == Regexp.new("^eee$") # => true
p...
<< 1 2 3 ... > >>