Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

書籍 Regular Expressions Cookbook | Validate Handy Phone Number

パンくず

書籍 Regular Expressions Cookbook
Validate Handy Phone Number

概要

携帯電話番号のバリデーション

サンプル

subject = Array.new
subject << "08012345678"
subject << "090-1234-5678"
subject << "070-1234-5678"
subject << " 090-1234-5678"
subject << "090-1234-56781"
# => 1,2番目の要素のみマッチ
pattern = /^(?:0[89]0)[-]?(?:[\d]{4})[-]?(?:\d{4})$/
subject.each_with_index {|each_subject,i|puts "#{i}:#{each_subject.scan(pattern)}"}

結果

0:["08012345678"]
1:["090-1234-5678"]
2:[]
3:[]
4:[]