Skip to content

Instantly share code, notes, and snippets.

@xu1718191411
Created March 23, 2018 02:27
Show Gist options
  • Save xu1718191411/3c79d6c7608aeeac8536c4ec2f5156e4 to your computer and use it in GitHub Desktop.
Save xu1718191411/3c79d6c7608aeeac8536c4ec2f5156e4 to your computer and use it in GitHub Desktop.
正規表現で日本の住所を解析する ref: https://qiita.com/xu1718191411/items/72b1454488a8abfd3aad
String matchString = "〒066-0012\n" +
"北海道千歳市美々 新千歳空港国内線ターミナルビル2F";
Matcher matcher = Pattern.compile("\\s*〒(\\d{3}-\\d{4})[\\s ]*(.*?都)?(.*?道)?(.*?府)?(.*?県)?(.*?市)?(.*?区)?").matcher(matchString);
while (matcher.find()){
System.out.println("住所:" + matcher.group(0));
System.out.println();
System.out.println("郵便番号:" + matcher.group(1));
System.out.println();
System.out.println("都名:" + matcher.group(2));
System.out.println();
System.out.println("道名:" + matcher.group(3));
System.out.println();
System.out.println("府名:" + matcher.group(4));
System.out.println();
System.out.println("県名:" + matcher.group(5));
System.out.println();
System.out.println("市名:" + matcher.group(6));
System.out.println();
System.out.println("区名" + matcher.group(7));
}
住所:〒066-0012
北海道千歳市
郵便番号:066-0012
都名:null
道名:北海道
府名:null
県名:null
市名:千歳市
区名null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment