「Ruby」正規表現式のサンプル

書式
~ /文字列(.*)/ )
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/usr/bin/ruby
cftA = "Cats are smarter than dogs";
cftB = "Dogs also like meat";
if ( cftA =~ /Cats(.*)/ )
puts "文字列A contains Cats"
end
if (cftB =~ /Cats(.*)/ )
puts "文字列B contains Dogs"
end
#!/usr/bin/ruby cftA = "Cats are smarter than dogs"; cftB = "Dogs also like meat"; if ( cftA =~ /Cats(.*)/ ) puts "文字列A contains Cats" end if (cftB =~ /Cats(.*)/ ) puts "文字列B contains Dogs" end
#!/usr/bin/ruby

cftA = "Cats are smarter than dogs";
cftB = "Dogs also like meat";

if ( cftA =~ /Cats(.*)/ )
  puts "文字列A contains Cats"
end
if (cftB =~ /Cats(.*)/ )
  puts "文字列B contains  Dogs"
end

実行結果
文字列A contains Cats

Ruby

Posted by arkgame