るりまサーチ

最速Rubyリファレンスマニュアル検索!
240件ヒット [1-100件を表示] (0.066秒)
トップページ > クエリ:ruby[x] > クエリ:p[x] > 種類:インスタンスメソッド[x] > クラス:MatchData[x]

別のキーワード

  1. rbconfig ruby
  2. fiddle ruby_free
  3. fiddle build_ruby_platform
  4. rake ruby
  5. rubygems/defaults ruby_engine

ライブラリ

キーワード

検索結果

<< 1 2 3 > >>

MatchData#regexp -> Regexp (6208.0)

自身の元になった正規表現オブジェクトを返します。

...自身の元になった正規表現オブジェクトを返します。

//emlist[例][ruby]{
m = /a.*b/.match("abc")
m.regexp # => /a.*b/
//}...

MatchData#captures -> [String] (6120.0)

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

...す。

MatchData
#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...
...=> ["foo", "bar", nil]
//}

@see MatchData#to_a, MatchData#named_captures, d:spec/pattern_matching#matching_non_primitive_objects...

MatchData#post_match -> String (6114.0)

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

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

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

@see MatchData#pre_match...

MatchData#pre_match -> String (6114.0)

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

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

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

@see MatchData#post_match...

MatchData#inspect -> String (6108.0)

self の内容を人間に読みやすい文字列にして返します。

...][ruby]{
p
uts /.$/.match("foo").inspect
# => #<MatchData "o">

p
uts /(.)(.)(.)/.match("foo").inspect
# => #<MatchData "foo" 1:"f" 2:"o" 3:"o">

p
uts /(.)(.)?(.)/.match("fo").inspect
# => #<MatchData "fo" 1:"f" 2:nil 3:"o">

p
uts /(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge").inspect
# => #<MatchData "h...

絞り込み条件を変える

MatchData#named_captures -> Hash (6108.0)

名前付きキャプチャをHashで返します。

...@param symbolize_names 真を指定するとハッシュのキーを文字列ではなくシンボルにします。デフォルトは偽です。

//emlist[例][ruby]{
m = /(?<a>.)(?<b>.)/.match("01")
m.named_captures # => {"a" => "0", "b" => "1"}

m = /(?<a>.)(?<b>.)?/.match("0")
m.named_capture...
...s # => {"a" => "0", "b" => nil}

m = /(?<a>.)(?<a>.)/.match("01")
m.named_captures # => {"a" => "1"}

m = /(?<a>x)|(?<a>y)/.match("x")
m.named_captures # => {"a" => "x"}
//}

@see MatchData#captures...
...s # => {"a" => "0", "b" => nil}

m = /(?<a>.)(?<a>.)/.match("01")
m.named_captures # => {"a" => "1"}

m = /(?<a>x)|(?<a>y)/.match("x")
m.named_captures # => {"a" => "x"}
//}

@see MatchData#captures, MatchData#deconstruct_keys...

MatchData#named_captures(symbolize_names: false) -> Hash (6108.0)

名前付きキャプチャをHashで返します。

...@param symbolize_names 真を指定するとハッシュのキーを文字列ではなくシンボルにします。デフォルトは偽です。

//emlist[例][ruby]{
m = /(?<a>.)(?<b>.)/.match("01")
m.named_captures # => {"a" => "0", "b" => "1"}

m = /(?<a>.)(?<b>.)?/.match("0")
m.named_capture...
...", "b" => nil}

m = /(?<a>.)(?<a>.)/.match("01")
m.named_captures # => {"a" => "1"}

m = /(?<a>x)|(?<a>y)/.match("x")
m.named_captures # => {"a" => "x"}

m = /(?<a>.)(?<a>.)/.match("01")
m.named_captures(symbolize_names: true) #=> {:a => "1"}
//}

@see MatchData#captures, MatchData#deconstruct_keys...

MatchData#deconstruct -> [String] (3020.0)

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

...す。

MatchData
#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, d:spec/pattern_matching#matching_non_primitive_objects...

MatchData#bytebegin(n) -> Integer | nil (68.0)

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

...ルを渡した場合は、対応する名前付きキャプチャの先頭のバイトオフセットを返します。

@param n 部分文字列を指定する数値。
@param name 名前付きキャプチャを指定する文字列またはシンボル。

@raise IndexError 範囲外の n を指...
...発生します。

//emlist[例][ruby]{
/(c).*(いう).*(e.*)/ =~ 'abcあいうdef'
p
$~ # => #<MatchData "cあいうdef" 1:"c" 2:"いう" 3:"ef">
p
$~.bytebegin(0) # => 2
p
$~.bytebegin(1) # => 2
p
$~.bytebegin(2) # => 6
p
$~.bytebegin(3) # => 13
p
$~.bytebegin(4) # => index 4 out of...
...list[シンボルを指定する例][ruby]{
/(?<key>\S+):\s*(?<value>\S+)/ =~ "name: ruby"
$~ # => #<MatchData "name: ruby" key:"name" value:"ruby">
$~.bytebegin(:key) # => 0
$~.bytebegin(:value) # => 6
$~.bytebegin(:foo) # => undefined group name reference: foo (IndexError)
//...

MatchData#bytebegin(name) -> Integer | nil (68.0)

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

...ルを渡した場合は、対応する名前付きキャプチャの先頭のバイトオフセットを返します。

@param n 部分文字列を指定する数値。
@param name 名前付きキャプチャを指定する文字列またはシンボル。

@raise IndexError 範囲外の n を指...
...発生します。

//emlist[例][ruby]{
/(c).*(いう).*(e.*)/ =~ 'abcあいうdef'
p
$~ # => #<MatchData "cあいうdef" 1:"c" 2:"いう" 3:"ef">
p
$~.bytebegin(0) # => 2
p
$~.bytebegin(1) # => 2
p
$~.bytebegin(2) # => 6
p
$~.bytebegin(3) # => 13
p
$~.bytebegin(4) # => index 4 out of...
...list[シンボルを指定する例][ruby]{
/(?<key>\S+):\s*(?<value>\S+)/ =~ "name: ruby"
$~ # => #<MatchData "name: ruby" key:"name" value:"ruby">
$~.bytebegin(:key) # => 0
$~.bytebegin(:value) # => 6
$~.bytebegin(:foo) # => undefined group name reference: foo (IndexError)
//...

絞り込み条件を変える

MatchData#byteend(n) -> Integer | nil (68.0)

n 番目の部分文字列終端のバイトオフセットを返します。

...ルを渡した場合は、対応する名前付きキャプチャの終端のバイトオフセットを返します。

@param n 部分文字列を指定する数値。
@param name 名前付きキャプチャを指定する文字列またはシンボル。

@raise IndexError 範囲外の n を指...
...合に発生します。

//emlist[例][ruby]{
/(c).*(いう).*(e.*)/ =~ 'abcあいうdef'
p
$~ # => #<MatchData "cあいうdef" 1:"c" 2:"いう" 3:"ef">
p
$~.byteend(0) # => 15
p
$~.byteend(1) # => 3
p
$~.byteend(2) # => 12
p
$~.byteend(3) # => 15
p
$~.byteend(4) # => index 4 out of matc...
.../emlist[シンボルを指定する例][ruby]{
/(?<key>\S+):\s*(?<value>\S+)/ =~ "name: ruby"
$~ # => #<MatchData "name: ruby" key:"name" value:"ruby">
$~.byteend(:key) # => 4
$~.byteend(:value) # => 10
$~.byteend(:foo) # => undefined group name reference: foo (IndexError)
//}...

MatchData#byteend(name) -> Integer | nil (68.0)

n 番目の部分文字列終端のバイトオフセットを返します。

...ルを渡した場合は、対応する名前付きキャプチャの終端のバイトオフセットを返します。

@param n 部分文字列を指定する数値。
@param name 名前付きキャプチャを指定する文字列またはシンボル。

@raise IndexError 範囲外の n を指...
...合に発生します。

//emlist[例][ruby]{
/(c).*(いう).*(e.*)/ =~ 'abcあいうdef'
p
$~ # => #<MatchData "cあいうdef" 1:"c" 2:"いう" 3:"ef">
p
$~.byteend(0) # => 15
p
$~.byteend(1) # => 3
p
$~.byteend(2) # => 12
p
$~.byteend(3) # => 15
p
$~.byteend(4) # => index 4 out of matc...
.../emlist[シンボルを指定する例][ruby]{
/(?<key>\S+):\s*(?<value>\S+)/ =~ "name: ruby"
$~ # => #<MatchData "name: ruby" key:"name" value:"ruby">
$~.byteend(:key) # => 4
$~.byteend(:value) # => 10
$~.byteend(:foo) # => undefined group name reference: foo (IndexError)
//}...

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

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

...返します。

@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
$~[3] # =...
...> nil (マッチしていない)
p
$~[4] # => nil (範囲外)
p
$~[-2] # => "bar"
//}...

MatchData#offset(name) -> [Integer, Integer] | [nil, nil] (51.0)

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

...の配列 [start, end] を返
します。

//emlist[例][ruby]{
[ self.begin(name), self.end(name) ]
//}

と同じです。nameの名前付きグループにマッチした部分文字列がなければ
[nil, nil] を返します。

@param name 名前(シンボルか文字列)

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

@see MatchData#begin, MatchData#end...
...ry') # => `offset': undefined group name reference: century (IndexError)
//}

@see MatchData#begin, MatchData#end, MatchData#offset...
<< 1 2 3 > >>