我让用户提交电话号码。对其输入方式没有限制。 从这个电话号码我需要的是两件事; areacode(不包括前导0)和其余的phonenumber。两者都没有非数字字符。
用户可能输入的可能情况:
我想得到的结果(两个NSStrings或int或者其他):
用户输入:0707123456
Var 1:70
变量2:7123456
用户输入:+46(0)8 12 345 67 8
Var 1:8
变量2:12345678
这些是区号:
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 < / p>
如何使用Objective-c执行此操作?
答案 0 :(得分:2)
我对Objective-c不太熟悉,但应该解析所有提供的示例条目,假设国家代码为+46
或没有国家/地区代码,并且支持列出的所有区号,从而得出这些结果:
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[ -]*)+)
一个限制是区号必须是连续的数字(即070
或70
,但不是0 7-0
或0 7 0
)
首先匹配国家/地区代码(如果存在),并将其存储在反向引用1中,然后忽略单个零。然后它将匹配其中一个区号并将其存储在反向引用2.它将获得任意数量的数字(一个或多个),混合使用短划线(-
)和空格(并将这些存储到反向引用3
要在最后一部分中获得一个实数(即从12345678
获取123 45-6-78
),您需要运行另一个查找和替换以删除空格和连字符/破折号。它可以是传统的纯文本查找替换,也可以使用正则表达式:[ -]
(替换为零长度字符串)