「Ruby入門」matchメソッドで部分一致文字列を取得するサンプル

書式
match(*検索文字列)
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
strA = "Study Ruby Skill in arkgame com## y test"
res = strA.match(/.*Ruby/)
puts "結果1: "+res[0]
res2 = strA.match(/.*##/)
puts "結果2: "+res2[0]
strA = "Study Ruby Skill in arkgame com## y test" res = strA.match(/.*Ruby/) puts "結果1: "+res[0] res2 = strA.match(/.*##/) puts "結果2: "+res2[0]
strA = "Study  Ruby Skill in arkgame com## y test"

res = strA.match(/.*Ruby/)
 
puts "結果1: "+res[0]

res2 = strA.match(/.*##/)
 
puts "結果2: "+res2[0]

実行結果
>ruby test.rb
結果1: Study Ruby
結果2: Study Ruby Skill in arkgame com##

Ruby

Posted by arkgame