るりまサーチ

最速Rubyリファレンスマニュアル検索!
3件ヒット [1-3件を表示] (0.013秒)
トップページ > クラス:Regexp[x] > クエリ:MatchData#captures[x]

ライブラリ

検索結果

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

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

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

省略可能な第二引数 pos を指定すると、...
...ォルト値は 0)。

p(/(.).(.)/.match("foobar", 3).captures) # => ["b", "r"]
p(/(.).(.)/.match("foobar", -3).captures) # => ["b", "r"]

pos を指定しても MatchData#offset 等の結果
には影響しません。つまり、
re.match(str[pos..-1])

re.match(str, pos)
は異な...
...ら行うよう制御できます(pos のデフォルト値は 0)。

使用例

reg = Regexp.new("foo")

if reg.match("foobar")
print "match\n" #=> match
end

p reg.match("foobar") #=> #<MatchData:0x29403fc>
p reg.match("bar") #=> nil

p /(foo)(bar)(baz)/.match("foobarbaz").to_a....

Regexp#match(str) -> MatchData | nil (166)

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

...。マッチした場合には結果を MatchData オブジェクトで返します。
マッチしなかった場合 nil を返します。


@param str 文字列を指定します。str との正規表現マッチを行います。


使用例

reg = Regexp.new("foo")

if reg.match("foobar")...
...用途に MatchData#captures が使
えると考えるかも知れませんが、captures では、マッチに失敗した場合、
nil.captures を呼び出そうとして例外 NoMethodError が発生して
しまいます。

foo, bar, baz = /(foo)(bar)(baz)/.match("foobar").captures

# => -...
...:1: undefined method `captures' for nil:NilClass (NoMethodError)...