从电话号码获取区号 [英] Getting an areacode from the phone number

查看:164
本文介绍了从电话号码获取区号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让用户提交电话号码.输入方式没有任何限制. 我需要这个电话号码有两件事:区号(不包括前导0)和电话号码的其余部分.两者都没有非数字字符.

I am letting a user submit a phonenumber. There are no restrictions on how this is entered. What I need from this phone number are two things; The areacode (excluding the leading 0) and the rest of the phonenumber. Both without an non-digit chars.

用户可能输入的可能方案:

Possible scenarios the user might enter:

  • 0707123456
  • 0707-123456
  • 0707-12 34 56
  • 08-123 456 78
  • 08-123 45 67
  • 031-123 45 67
  • 031-12 34 56
  • 0480-12 34 56
  • 0480-123 45
  • +468 123 456 78
  • +46(0)8 12 345 67 8
  • 0707123456
  • 0707-123456
  • 0707-12 34 56
  • 08-123 456 78
  • 08-123 45 67
  • 031-123 45 67
  • 031-12 34 56
  • 0480-12 34 56
  • 0480-123 45
  • +468 123 456 78
  • +46 (0) 8 12 345 67 8

我想要得到的结果(两个NSStrings或int或其他): 用户输入:0707123456
Var 1:70
Var 2:7123456

用户输入:+46(0)8 12 345 67 8
Var 1:8
Var 2:12345678

这些是区号:

Result I want to get (two NSStrings or int or whatever): User enters: 0707123456
Var 1: 70
Var 2: 7123456

User enters: +46 (0) 8 12 345 67 8
Var 1: 8
Var 2: 12345678

These are the area codes:

70、730、76、271、322、174、472、371、589、961、960、570、583、226、624、915、531、652、932、662、921、278、243、33 ,142、661、456、693、914、912、431、571、295、586、552、653、942、534、271、381、471、171、246、16、413、223、346、515、23 ,590、122、585、157、241、943、684、258、528、645、241、493、371、158、498、525、555、591、390、514、551、672、970、693、26 ,31,511,563,975,643,582,220,175,35,696,644 297,922,928,224,684,225,291,42,513,301,503,290,671,506 ,650,495,36,663,345,591,611,451,42,491,415,413,253,247,971,621,647,36,916,923,480,505,454,294,586 ,455,54,150,554,320,980,494,435,580,977,612,44,550,640,226,19,300,227,303,221,430,925,418,584,247 ,474,302,478,692,510,581,13,642,372,651,657,240,920,46,950,523,913,157,40,280,953,496,159,501,433 ,530,142,553,250,141,392,524,563,499,587,223,930,11,176,918,512,481,155,380,622,297,454,250,304,479 ,491,643,155,978,4 35、911、973、623、175、934、457、459、472、924、682、248、224、26、414、416、511、910、222、142、294、500、240、620、952, 8,951,290,152,433,526,670,695,60,565,220,418,585,680,325,687,246,564,533,253,225,382,293,270,121, 456、504、502、293、60、477、304、417、691、560、16、486、345、325、140、410、520、156、954、292、506、522、613、321、90, 18、143、393、156、123、281、512、340、383、125、940、492、933、151、495、498、981、976、322、521、935、370、490、21、470, 411、571、532、690、647、573、474、941、120、476、251、929、431、144、485、295、19、173、660、291、63、292、173、926、927

70, 730, 76, 271, 322, 174, 472, 371, 589, 961, 960, 570, 583, 226, 624, 915, 531, 652, 932, 662, 921, 278, 243, 33, 142, 661, 456, 693, 914, 912, 431, 571, 295, 586, 552, 653, 942, 534, 271, 381, 471, 171, 246, 16, 413, 223, 346, 515, 23, 590, 122, 585, 157, 241, 943, 684, 258, 528, 645, 241, 493, 371, 158, 498, 525, 555, 591, 390, 514, 551, 672, 970, 693, 26, 31, 511, 563, 975, 643, 582, 220, 175, 35, 696, 644, 297, 922, 928, 224, 684, 225, 291, 42, 513, 301, 503, 290, 671, 506, 650, 495, 36, 663, 345, 591, 611, 451, 42, 491, 415, 413, 253, 247, 971, 621, 647, 36, 916, 923, 480, 505, 454, 294, 586, 455, 54, 150, 554, 320, 980, 494, 435, 580, 977, 612, 44, 550, 640, 226, 19, 300, 227, 303, 221, 430, 925, 418, 584, 247, 474, 302, 478, 692, 510, 581, 13, 642, 372, 651, 657, 240, 920, 46, 950, 523, 913, 157, 40, 280, 953, 496, 159, 501, 433, 530, 142, 553, 250, 141, 392, 524, 563, 499, 587, 223, 930, 11, 176, 918, 512, 481, 155, 380, 622, 297, 454, 250, 304, 479, 491, 643, 155, 978, 435, 911, 973, 623, 175, 934, 457, 459, 472, 924, 682, 248, 224, 26, 414, 416, 511, 910, 222, 142, 294, 500, 240, 620, 952, 8, 951, 290, 152, 433, 526, 670, 695, 60, 565, 220, 418, 585, 680, 325, 687, 246, 564, 533, 253, 225, 382, 293, 270, 121, 456, 504, 502, 293, 60, 477, 304, 417, 691, 560, 16, 486, 345, 325, 140, 410, 520, 156, 954, 292, 506, 522, 613, 321, 90, 18, 143, 393, 156, 123, 281, 512, 340, 383, 125, 940, 492, 933, 151, 495, 498, 981, 976, 322, 521, 935, 370, 490, 21, 470, 411, 571, 532, 690, 647, 573, 474, 941, 120, 476, 251, 929, 431, 144, 485, 295, 19, 173, 660, 291, 63, 292, 173, 926, 927

如何使用Objective-c做到这一点?

How can I do this with Objective-c?

推荐答案

我对Objective-c不太熟悉,但是答案应该能够解析所有提供的示例条目,并假设国家/地区代码为+46或没有国家/地区代码,并且支持列出的所有区号,因此会得到以下结果:

I am not very familiar with Objective-c, but an answer that should parse all of the provided example entries, assuming either a country code of +46 or no country code, and supporting all of the area codes listed, thus giving these results:

input                              (1)     (2)     (3)
---------------------              ------  ------  ---------------
0707123456                                 70      7123456
0707-123456                                70      7-123456
0707-12 34 56                              70      7-12 34 56
08-123 456 78                              8       123 456 78
08-123 45 67                               8       123 45 67
031-123 45 67                              31      123 45 67
031-12 34 56                               31      12 34 56
0480-12 34 56                              480     12 34 56
0480-123 45                                480     123 45
+468 123 456 78                    +46     8       123 456 78
+46 (0) 8 12 345 67 8              +46     8       12 345 67 8

是这个表达式:

^[ -]*(\+46)?[ -]*[(0)]*[ -]*(70|730|76|271|322|174|472|371|589|961|960|570|583|226|624|915|531|652|932|662|921|278|243|33|142|661|456|693|914|912|431|571|295|586|552|653|942|534|271|381|471|171|246|16|413|223|346|515|23|590|122|585|157|241|943|684|258|528|645|241|493|371|158|498|525|555|591|390|514|551|672|970|693|26|31|511|563|975|643|582|220|175|35|696|644|297|922|928|224|684|225|291|42|513|301|503|290|671|506|650|495|36|663|345|591|611|451|42|491|415|413|253|247|971|621|647|36|916|923|480|505|454|294|586|455|54|150|554|320|980|494|435|580|977|612|44|550|640|226|19|300|227|303|221|430|925|418|584|247|474|302|478|692|510|581|13|642|372|651|657|240|920|46|950|523|913|157|40|280|953|496|159|501|433|530|142|553|250|141|392|524|563|499|587|223|930|11|176|918|512|481|155|380|622|297|454|250|304|479|491|643|155|978|435|911|973|623|175|934|457|459|472|924|682|248|224|26|414|416|511|910|222|142|294|500|240|620|952|8|951|290|152|433|526|670|695|60|565|220|418|585|680|325|687|246|564|533|253|225|382|293|270|121|456|504|502|293|60|477|304|417|691|560|16|486|345|325|140|410|520|156|954|292|506|522|613|321|90|18|143|393|156|123|281|512|340|383|125|940|492|933|151|495|498|981|976|322|521|935|370|490|21|470|411|571|532|690|647|573|474|941|120|476|251|929|431|144|485|295|19|173|660|291|63|292|173|926|927)[ -]*((?:\d[ -]*)+)

一个限制是区号必须是连续的数字(即07070,但不能是0 7-00 7 0)

The one restriction is that the area code must be contiguous digits (i.e. 070 or 70, but not 0 7-0 or 0 7 0)

它将首先匹配国家/地区代码(如果存在),并将其存储在反向引用1中,然后忽略单个零.然后,它将匹配一个区号并将其存储在反向引用2中.然后它将得到任意数量的数字(一个或多个),并与破折号(-)和空格()混合并存储这些数字进入反向引用3

It will first match the country code, if it is present, and store it in back-reference 1, then ignore a single zero. It will then match one of the area codes and store it in back-reference 2. It will then get any number of digits (one or more), mixed with dashes (-) and spaces () and store those into back-reference 3

要在最后一部分得到一个实数(即从123 45-6-78获得12345678),您将需要运行另一个查找并替换以消除空格和连字符/破折号.它既可以是传统的纯文本查找替换,也可以使用正则表达式,例如:[ -](用零长度的字符串替换)

To get a solid number in the last part (i.e. get 12345678 from 123 45-6-78), you will need to run another find and replace to get rid of spaces and hyphens/dashes. It can either be a either traditional text-only find-replace, or it could use a regex like: [ -] (replaced with a zero-length string)

这篇关于从电话号码获取区号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆