るりまサーチ

最速Rubyリファレンスマニュアル検索!
191件ヒット [1-100件を表示] (0.151秒)

別のキーワード

  1. _builtin new
  2. _builtin inspect
  3. _builtin []
  4. _builtin to_s
  5. _builtin each

ライブラリ

クラス

キーワード

検索結果

<< 1 2 > >>

Regexp#=~(string) -> Integer | nil (26167.0)

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

...場合には nil を返
します。

//emlist[例][ruby]{
p /foo/ =~ "foo" # => 0
p /foo/ =~ "afoo" # => 1
p /foo/ =~ "bar" # => nil
//}

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

文字列のかわりにSymbolをマッ...
.../emlist[例][ruby]{
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
pr...

MatchData#post_match -> String (17120.0)

マッチした部分より後ろの文字列を返します($'と同じ)。

...マッチした部分より後ろの文字列を返します($'と同じ)。

//emlist[例][ruby]{
/(bar)(BAZ)?/ =~ "foobarbaz"
p $~.post_match # => "baz"
//}

@see MatchData#pre_match...

MatchData#pre_match -> String (17120.0)

マッチした部分より前の文字列を返します($`と同じ)。

...マッチした部分より前の文字列を返します($`と同じ)。

//emlist[例][ruby]{
/(bar)(BAZ)?/ =~ "foobarbaz"
p $~.pre_match # => "foo"
//}

@see MatchData#post_match...

MatchData#[](name) -> String | nil (11016.0)

name という名前付きグループにマッチした文字列を返します。

...m name 名前(シンボルか文字列)
@raise IndexError 指定した名前が正規表現内に含まれていない場合に発生します

//emlist[例][ruby]{
/\$(?<dollars>\d+)\.(?<cents>\d+)/.match("$3.67")[:cents] # => "67"
/(?<alpha>[a-zA-Z]+)|(?<num>\d+)/.match("aZq")[:num] # => nil
//}...

MatchData#[](n) -> String | nil (11011.0)

n 番目の部分文字列を返します。

...時には nil を返します。

@param n 返す部分文字列のインデックスを指定します。

//emlist[例][ruby]{
/(foo)(bar)(BAZ)?/ =~ "foobarbaz"
p $~.to_a # => ["foobar", "foo", "bar", nil]
p $~[0] # => "foobar"
p $~[1] # => "foo"
p $~[2] # => "bar"
p...

絞り込み条件を変える

MatchData#[](range) -> [String] (11011.0)

Range オブジェクト range の範囲にある要素からなる部分配列を返します。

...Range オブジェクト range の範囲にある要素からなる部分配列を返します。

@param range start..end 範囲式。

//emlist[例][ruby]{
/(foo)(bar)/ =~ "foobarbaz"
p $~[0..2] # => ["foobar", "foo", "bar"]
//}...

MatchData#[](start, length) -> [String] (11011.0)

start 番目から length 個の要素を含む部分配列を返します。

...start 番目から length 個の要素を含む部分配列を返します。

//emlist[例][ruby]{
/(foo)(bar)/ =~ "foobarbaz"
p $~[0, 3] # => ["foobar", "foo", "bar"]
//}

@see Array#[]...

MatchData#begin(n) -> Integer | nil (11007.0)

n 番目の部分文字列先頭のオフセットを返します。

...範囲外の n を指定した場合に発生します。

//emlist[例][ruby]{
/(foo)(bar)(BAZ)?/ =~ "foobarbaz"
p $~.begin(0) # => 0
p $~.begin(1) # => 0
p $~.begin(2) # => 3
p $~.begin(3) # => nil
p $~.begin(4) # => `begin': index 4 out of matches (IndexError)
//}

@see MatchData#end...

MatchData#byteoffset(name) -> [Integer, Integer] | [nil, nil] (11007.0)

name という名前付きグループに対応する部分文字列のバイト単位のオフセットの 配列 [start, end] を返します。

...れていない name を指定した場合に発生します。

//emlist[例][ruby]{
/(?<year>\d{4})年(?<month>\d{1,2})月(?:(?<day>\d{1,2})日)?/ =~ "2021年1月"
p $~.byteoffset('year') # => [0, 4]
p $~.byteoffset(:year) # => [0, 4]
p $~.byteoffset('month') # => [7, 8]
p $~.byteoffset(:...
...month) # => [7, 8]
p $~.byteoffset('day') # => [nil, nil]
p $~.byteoffset('century') # => `offset': undefined group name reference: century (IndexError)
//}

@see MatchData#offset...

MatchData#captures -> [String] (11007.0)

$1, $2, ... を格納した配列を返します。

...... を格納した配列を返します。

Match
Data#to_a と異なり $& を要素に含みません。
グループにマッチした部分文字列がなければ対応する要素は nil になります。

//emlist[例][ruby]{
/(foo)(bar)(BAZ)?/ =~ "foobarbaz"
p $~.to_a # => ["foobar",...
..."foo", "bar", nil]
p $~.captures # => ["foo", "bar", nil]
//}

@see MatchData#to_a, MatchData#named_captures...

絞り込み条件を変える

<< 1 2 > >>