Tbpgr Blog

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

書籍 Regular Expressions Cookbook | Limit the Length of Text

パンくず

書籍 Regular Expressions Cookbook
Limit the Length of Text

概要

文字数のバリデーション。

サンプル

# encoding: Windows-31J

require "pp"

subject = Array.new
subject << "azA0"
subject << "azAZ0"
subject << "azAZ01234あ"
subject << "azAZ012345あ"

pattern = /^.{5,10}$/ # => 文字数5−10文字チェック
subject.each_with_index {|each_subject,i|puts "#{i}:#{each_subject.scan(pattern)}"}

結果

0:[]
1:["azAZ0"]
2:["azAZ01234あ"]
3:[]