6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

正規表現で住所情報から都道府県の情報のみを取り出したい

Last updated at Posted at 2016-03-24

例: 以下のような住所情報が格納された配列があり、その中から都道府県の情報をのみを配列として取得したい場合。

["神奈川県横浜市港北区", "東京都港区赤坂", "京都府京都市右京区"].map{|address| address.match(/^(.{1,3}[府|都|道|県])/);$1}
=> ["神奈川県", "東京都", "京都府"]

逆に都道府県の情報を除外した住所情報が欲しい場合には以下の様にする。

["神奈川県横浜市港北区", "東京都港区赤坂", "京都府京都市右京区"].map{|address| address.match(/^(.{1,3}[都|道|府|県])(\S+)/);$2}
=> ["横浜市港北区", "港区赤坂", "京都市右京区"]
6
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?