Tbpgr Blog

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

書籍 Regular Expressions Cookbook | Limit Input to Alphanumeric Characters

パンくず

書籍 Regular Expressions Cookbook
Limit Input to Alphanumeric Characters

概要

半角英数字のバリデーション。

サンプル

subject = Array.new
subject << "azAZ09" # =>match
subject << "azAZあ09"
subject << ""
subject << "" # =>match

pattern = /^[A-Za-z\d]*?$/
subject.each_with_index {|each_subject,i|puts "#{i}:#{each_subject.scan(pattern)}"}

puts "------------------------------------------------------"

pattern = /^[A-Z\d]*?$/i
subject.each_with_index {|each_subject,i|puts "#{i}:#{each_subject.scan(pattern)}"}

puts "------------------------------------------------------"

pattern = /^\w*?$/i
subject.each_with_index {|each_subject,i|puts "#{i}:#{each_subject.scan(pattern)}"}

結果

0:["azAZ09"]
1:[]
2:[]
3:[]
------------------------------------------------------
0:["azAZ09"]
1:[]
2:[]
3:[]
------------------------------------------------------
0:["azAZ09"]
1:[]
2:[]
3:[]