Tbpgr Blog

元エンジニア 人事 tbpgr(てぃーびー) のブログ

書籍 Regular Expressions Cookbook | Validating URLs

パンくず

書籍 Regular Expressions Cookbook
Validating URLs

概要

URLのValidationについて

サンプル

subject = Array.new
subject << "http://test.com" # => match
subject << "https://test.cpm" # => match
subject << "ftp://hoge" # => match
subject << "file://hoge" # => match
subject << "ntp://hoge"
pattern = /\A(https?|ftp|file):\/\/.+/
subject.each_with_index {|each_subject,i|puts "#{i}:#{each_subject.scan(pattern)}"}

出力

0:[["http"]]
1:[["https"]]
2:[["ftp"]]
3:[["file"]]
4:[]