213件ヒット
[1-100件を表示]
(0.050秒)
別のキーワード
検索結果
先頭5件
-
MatchData
# regexp -> Regexp (6203.0) -
自身の元になった正規表現オブジェクトを返します。
...自身の元になった正規表現オブジェクトを返します。
//emlist[例][ruby]{
m = /a.*b/.match("abc")
m.regexp # => /a.*b/
//}... -
MatchData
# captures -> [String] (6115.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... -
MatchData
# post _ match -> String (6109.0) -
マッチした部分より後ろの文字列を返します($'と同じ)。
...マッチした部分より後ろの文字列を返します($'と同じ)。
//emlist[例][ruby]{
/(bar)(BAZ)?/ =~ "foobarbaz"
p $~.post_match # => "baz"
//}
@see MatchData#pre_match... -
MatchData
# pre _ match -> String (6109.0) -
マッチした部分より前の文字列を返します($`と同じ)。
...マッチした部分より前の文字列を返します($`と同じ)。
//emlist[例][ruby]{
/(bar)(BAZ)?/ =~ "foobarbaz"
p $~.pre_match # => "foo"
//}
@see MatchData#post_match... -
MatchData
# inspect -> String (6103.0) -
self の内容を人間に読みやすい文字列にして返します。
...ruby]{
puts /.$/.match("foo").inspect
# => #<MatchData "o">
puts /(.)(.)(.)/.match("foo").inspect
# => #<MatchData "foo" 1:"f" 2:"o" 3:"o">
puts /(.)(.)?(.)/.match("fo").inspect
# => #<MatchData "fo" 1:"f" 2:nil 3:"o">
puts /(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge").inspect
# => #<MatchData "hog... -
MatchData
# named _ captures -> Hash (6103.0) -
名前付きキャプチャをHashで返します。
...01")
m.named_captures # => {"a" => "0", "b" => "1"}
m = /(?<a>.)(?<b>.)?/.match("0")
m.named_captures # => {"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
# [](n) -> String | nil (47.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
# byteoffset(name) -> [Integer , Integer] | [nil , nil] (39.0) -
name という名前付きグループに対応する部分文字列のバイト単位のオフセットの 配列 [start, end] を返します。
...@param name 名前(シンボルか文字列)
@raise IndexError 正規表現中で定義されていない 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
# offset(name) -> [Integer , Integer] | [nil , nil] (39.0) -
name という名前付きグループに対応する部分文字列のオフセットの配列 [start, end] を返 します。
...@param name 名前(シンボルか文字列)
@raise IndexError 正規表現中で定義されていない name を指定した場合に発生します。
//emlist[例][ruby]{
/(?<year>\d{4})年(?<month>\d{1,2})月(?:(?<day>\d{1,2})日)?/ =~ "2021年1月"
p $~.offset('year') # => [0, 4]
p $~.o......ffset(:year) # => [0, 4]
p $~.offset('month') # => [5, 6]
p $~.offset(:month) # => [5, 6]
p $~.offset('day') # => [nil, nil]
p $~.offset('century') # => `offset': undefined group name reference: century (IndexError)
//}
@see MatchData#begin, MatchData#end......ffset(:year) # => [0, 4]
p $~.offset('month') # => [5, 6]
p $~.offset(:month) # => [5, 6]
p $~.offset('day') # => [nil, nil]
p $~.offset('century') # => `offset': undefined group name reference: century (IndexError)
//}
@see MatchData#begin, MatchData#end, MatchData#offset... -
MatchData
# begin(n) -> Integer | nil (33.0) -
n 番目の部分文字列先頭のオフセットを返します。
...ます。
@param n 部分文字列を指定する数値。
@raise IndexError 範囲外の 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...