问题
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
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, 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
How can I do this with Objective-c?
回答1:
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
is this expression:
^[ -]*(\+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[ -]*)+)
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
)
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
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)
来源:https://stackoverflow.com/questions/8188964/getting-an-areacode-from-the-phone-number