0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

自分用rubyメモ パターンマッチングについて(随時追記)

0
Last updated at Posted at 2016-06-14

パターンマッチングを試してみる

regexp.rb
p /abc/ =~ "AbCabcAB"
p /abc/i =~ "AbCabcAB"
p /abc/ =~ "foobar"
p Regexp.new("abc")
result
3
0
nil
/abc/

結論

  • /パターン/ =~ "文字列"の形で正規表現によるパターンマッチングが可能。
  • /パターン/iとすると、大文字小文字関係なくマッチングする。
  • 見つからない場合nilを返す。rubyではや**-1真値**になるのでそのままif文に突っ込もう!
  • Regexp.new("文字列")でパターンの部分を作成可能。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?