5
3

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 1 year has passed since last update.

電話番号の正規化

Last updated at Posted at 2023-02-03

電話番号のハイフン位置まで含めて正規化したい

開発者が仕様書どころか使用方法のドキュメントすら作らずに逃げてしまたクソシステム、担当外だったのでずっと放置していたらとうとう「データベースがぐちゃぐちゃで使い物にならない」と苦情が来た。(担当してた開発者はもう居ない・・・)

電話番号にはバリデーションチェックもされないまま全角文字でもハイフンっぽい何かでもフリーダムにお構いなしに入っている・・・
とりあえずデータベースからデータを吸い上げて手入力でメンテナンスするもらちがあかないし、今後のメンテナンスもめんどくさい

・・・じゃあバッチ処理でパッチしてしまおう!で作ったモノの副産物です。
えいやっ!で作ったけどわりと便利だったので記事にしてみました。

※総務省が公表している 令和4年3月1日現在のデータ をもとに作成しています。

#!/bin/sh

## 文字種変換
TMP_TA=$( echo $1 | sed -e 's/[ー-‐‑–—―−ー]/-/g' \
   -e 's/0/0/g' -e 's/1/1/g' -e 's/2/2/g' \
   -e 's/3/3/g' -e 's/4/4/g' -e 's/5/5/g' \
   -e 's/6/6/g' -e 's/7/7/g' -e 's/8/8/g' \
   -e 's/9/9/g' -e 's/+81/0/g' -e 's/[^0-9+-\*]//g' )

## 0ABJ/携帯/050番号規則化
TMP_TB=$( echo ${TMP_TA} | sed -e 's/-//g' -e 's/(?![0-9])//g' \
   -e "s/^0\([5789]0\)\([1-9][0-9*]\{3\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\([0-9]\{2\}\0\)\([0-9*]\{3\}\)\([0-9*]\{3\}\)$/0\1-\2-\3/g" \
   -e "s/^0\([36]\)\([1-9][0-9*]\{3\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g"    \
   -e "s/^0\(11\)\([1-9][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(126\)\([1-689][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(1267\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(13[3-68]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(137\)\([1-689][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(1377\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(139\)\([13-69][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(139[278]\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(14[234]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(145\)\([1-589][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(145[67]\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(146\)\([1-5789][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(1466\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(15\)\([19][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(15[2378]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(154\)\([1-689][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(1547\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(155\)\([1-79][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(1558\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(156\)\([1235-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(1564\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(16[267]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(163\)\([136-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(163\)\([245]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(164\)\([1-79][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(1648\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(165\)\([1-379][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(165[4568]\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(17\)\([17][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(17[2-689]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(18\)\([189][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(18[2-7]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(19\)\([69][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(19[1-57]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(22\)\([127][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(22[03-689]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(23\)\([1269][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(23[34578]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(24\)\([59][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(24[0-4678]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(25\)\([123][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(25[04-9]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(26\)\(2[0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(26[013-9]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(27\)\([1235][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(27[046-9]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(28\)\([16][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(28[02-57-9]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(29\)\([238][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(29[14-79]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(4\)\([1][0-9]\{3\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(42\)\([13-79][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(42[28]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(43\)\([1-57][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(43[689]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(4[458]\)\([1-9][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(46\)\([12489][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(46[03567]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(47\)\([1-47][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(47[05689]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(49\)\([12678][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(49[345]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(499[2468]\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(52\)\([1-9][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(53\)\([45][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(53[1236-9]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(54\)\([12369][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(54[4578]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(55\)\([29][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(55[013-8]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(56[1-9]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(57[2-578]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(576\)\([1-8][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(5769\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(58\)\([2389][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(58[14-7]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(59\)\([123][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(59[4-689]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(597\)\([1-8][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(5979\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(72\)\([2346-9][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(72[15]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(73\)\([1-4][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(73[5-9]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(74[02-5789]\)\([1-4*][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(746\)\([1-79][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(7468\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g"\
   -e "s/^0\(7[58]\)\([1-9][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(76\)\([249][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(76[135-8]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(77\)\([57][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(77[0-4689]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(79\)\([23][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(79[014-9]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(82\)\([1258][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(82[034679]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(83\)\([12][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(83[3-7]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(838\)\([1-69][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(83\)\(9[0-9]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(8396\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g"\
   -e "s/^0\(84\)\([1-49][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(84[568]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(847\)\([1-689][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(8477\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g"\
   -e "s/^0\(851\)\([135-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(851[24]\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g"\
   -e "s/^0\(85[2-9]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(86\)\([124][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(86[356-9]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(87\)\([1-468][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(87[579]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(88\)\([1268][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(88[034579]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(89\)\([19][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(89[2-8]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(92\)\([1-9][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(920\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(93\)\([1-9][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(930\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(94[02346]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(949\)\([1-57-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(9496\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(95\)\([138][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(95[024-79]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(96\)\([123][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(96[4-9]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(97\)\([156][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(97[234789]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(98\)\([189][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(980\)\([13-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(9802\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g"\
   -e "s/^0\(98[2-7]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(99\)\([289][0-9*]\{2\}\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(99[3-57]\)\([1-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(991\)\([14-9][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(991[23]\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g"\
   -e "s/^0\(996\)\([1-8][0-9*]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" \
   -e "s/^0\(9969\)\([1-9]\)\([0-9*]\{4\}\)$/0\1-\2-\3/g" )

## 0ABJ/携帯/050番号規則チェック/規則外番号出力
if [ $(echo ${TMP_TB} | sed -e 's/[0-9+\*]//g' | wc -m ) -gt 2 ]; then
   # 0ABJ/携帯/050番号
   echo ${TMP_TB}
else
   # 規則外番号
   echo "${TMP_TA} [Incorrect]"
fi

海外の電話番号やでたらめな規則外番号は、とりあえず最低限の整形処理だけ行って出力します。シェルスクリプトの中に関数として組込んで使うと良いと思います。

5
3
0

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
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?