Skip to content

Instantly share code, notes, and snippets.

@labocho
Created January 20, 2017 05:30
Show Gist options
  • Select an option

  • Save labocho/54287be73fc7e8e8f5a4c93772696ed6 to your computer and use it in GitHub Desktop.

Select an option

Save labocho/54287be73fc7e8e8f5a4c93772696ed6 to your computer and use it in GitHub Desktop.
日本の住所を都道府県で分割するやつ
#!/usr/bin/env ruby
# Usage: echo "東京都xxx\n大阪府xxx\n名古屋市" | ./split_pref.rb
PREFECTURES = [
"北海道", "青森県", "岩手県", "宮城県", "秋田県",
"山形県", "福島県", "茨城県", "栃木県", "群馬県",
"埼玉県", "千葉県", "東京都", "神奈川県", "新潟県",
"富山県", "石川県", "福井県", "山梨県", "長野県",
"岐阜県", "静岡県", "愛知県", "三重県", "滋賀県",
"京都府", "大阪府", "兵庫県", "奈良県", "和歌山県",
"鳥取県", "島根県", "岡山県", "広島県", "山口県",
"徳島県", "香川県", "愛媛県", "高知県", "福岡県",
"佐賀県", "長崎県", "熊本県", "大分県", "宮崎県",
"鹿児島県", "沖縄県"
].inject({}){|hash, pref|
hash[pref] = Regexp.compile("^#{pref}")
hash
}
def split_pref(address)
PREFECTURES.each{|pref, regex|
if regex.match(address)
return [pref, address.gsub(regex, "")]
end
}
return ["", address]
end
ARGF.each_line do |address|
puts split_pref(address).join("\t")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment