Created
July 18, 2012 12:06
-
-
Save shizm/3135805 to your computer and use it in GitHub Desktop.
半角カタカナが含まれているか判別する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
def hankaku_kana_only?(str, options = { allow_kogaki: true, allow_dakuten: true, allow_kigou: true }) | |
# 半角カナのコード範囲内の文字だけで構成されているか | |
return false unless str =~ /\A(?:[\xEF\xBD\xA1-\xEF\xBD\xBF]|[\xEF\xBE\x80-\xEF\xBE\x9F])+\Z/ | |
unless options[:allow_kogaki] | |
# 小書き文字が含まれていないか | |
return false if str =~ /(?:[\xEF\xBD\xA7-\xEF\xBD\xAF])/ | |
end | |
unless options[:allow_dakuten] | |
# 濁点、半濁点が含まれてないか | |
return false if str =~ /(?:[\xEF\xBE\x9E|\xEF\xBE\x9F])/ | |
end | |
unless options[:allow_kigou] | |
# 記号が含まれていないか | |
return false if str =~ /(?:[\xEF\xBD\xA1-\xEF\xBD\xA5]|\xEF\xBD\xB0)/ | |
end | |
return true | |
end | |
puts hankaku_kana_only?('アイエウオ') | |
puts hankaku_kana_only?('アイウエオ') | |
puts hankaku_kana_only?('ァィゥェォ', allow_kogaki: false) | |
puts hankaku_kana_only?('パピプペポ') | |
puts hankaku_kana_only?('パピプペポ', allow_dakuten: false) | |
puts hankaku_kana_only?('ダヂヅデド') | |
puts hankaku_kana_only?('ダヂヅデド', allow_dakuten: false) | |
puts hankaku_kana_only?('。「」、') | |
puts hankaku_kana_only?('。「」、', allow_kigou: false) | |
puts hankaku_kana_only?('ー') | |
puts hankaku_kana_only?('ー', allow_kigou: false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment