12
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

【Ruby】住所から都道府県を正規表現で取り出す【正規表現】

住所から都道府県を取り出したい時ってありますよね!

そういう時はコレで。

address.match(/.*[都道府県]/)

例えば苫小牧高専なら…

tnct_address = "北海道苫小牧市字錦岡443番地" #我らが苫小牧高専の住所!
tnct_prefecture = tnct_address.match(/.*[都道府県]/).to_s
#=> "北海道"

[都道府県] って感じでグループ化してやることによって、'都'、'道'、'府'、'県'どれかでで終わる文字列を検索することができます。
べんり。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
12
Help us understand the problem. What are the problem?