Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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

そういう時はコレで。

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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした