argius note

プログラミング関連

正規表現の一例

久々のRuby。リファレンスマニュアルにまとめられているので敢えて書くこともないのですが、練習がてら書いておきます。

s = "ab12c345d"
puts s[/\d{3}/]    #=> 345
puts s.match(/\d/) #=> 1
re = /[^ab\d]/
puts re.match(s)   #=> c

これ↓を書いていて、Ruby正規表現ってやり過ぎ、と思いました。

a = ["2007/03/15 14:58:55", "2007/03/16 00:13:19"]
puts a[1][/:\d{2}:/][/\d+/] #=> 13