[Question]
  [
When making phone calls internationally, phone numbers are prefixed with a code indicating what country the number is located in. These codes are [prefix codes](https://en.wikipedia.org/wiki/Prefix_code), meaning that no code is a prefix of another.
Now, earlier today you missed a call, and you're kind of curious where that call might have come from. So you want to look up the calling code. But, being a prefix code, you're not quite sure where it ends, so you decide to write a program to separate the calling code from the rest of the number.
# Input
As input, you will recieve a string consisting of the digits `0-9`. The first few digits will be one of the country calling codes listed below (this means the first digit will never be `0`). After the country calling code, the rest of the input will contain zero or more digits in any order - it is *not* guaranteed to be a valid phone number. Your program must be able to handle inputs containing at least 15 digits
# Output
Your program should output the unique country calling code that is a prefix of the number. The valid outputs are as follows:
```
1
20
211
212
213
216
218
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
260
261
262
263
264
265
266
267
268
269
27
290
291
297
298
299
30
31
32
33
34
350
351
352
353
354
355
356
357
358
359
36
370
371
372
373
374
375
376
377
378
379
380
381
382
383
385
386
387
389
39
40
41
420
421
423
43
44
45
46
47
48
49
500
501
502
503
504
505
506
507
508
509
51
52
53
54
55
56
57
58
590
591
592
593
594
595
596
597
598
5993
5994
5997
5999
60
61
62
63
64
65
66
670
672
673
674
675
676
677
678
679
680
681
682
683
685
686
687
688
689
690
691
692
7
800
808
81
82
84
850
852
853
855
856
86
870
875
876
877
878
879
880
881
882
883
886
888
90
91
92
93
94
95
960
961
962
963
964
965
966
967
968
970
971
972
973
974
975
976
977
979
98
991
992
993
994
995
996
998
```
This list is based on the codes listed on Wikipedia's [list of country calling codes](https://en.wikipedia.org/wiki/List_of_country_calling_codes) page as of revision 915410826, with a few modifications
* All codes listed as unassigned or discontinued and some codes listed as reserved for future use were omitted
* If a code listed on Wikipedia is a prefix of another, the latter was omitted
* If a single country or territory would have more than one code, and if those codes would have a common prefix, those codes are omitted in favour of their common prefix.
This may result in independent countries being lumped together, or disputed territories being lumped in with a particular claimant. This is not intended as a political statement, and decisions about the inclusion or omission of territories and states were made based on the codes, not any beliefs I hold regarding the ownership or sovereignty of the entities using them.
If given an input that does not begin with any of these codes, your program's behaviour is undefined.
And finally:
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), fewer bytes of code is better
* Your submission may be either a function or a full program
* Any of the [default I/O methods](https://codegolf.meta.stackexchange.com/q/2447) are fine
* [The standard loopholes](https://codegolf.meta.stackexchange.com/q/1061) are forbidden
# Test cases
| input | output |
| --- | --- |
| 5292649259 | 52 |
| 3264296721 | 32 |
| 1550 | 1 |
| 33121394 | 33 |
| 7 | 7 |
| 2542112543 | 254 |
| 2005992972 | 20 |
| 350 | 350 |
| 360 | 36 |
| 8505234469 | 850 |
| 9795586334 | 979 |
| 148985513598795 | 1 |
| 222222 | 222 |
| 5999995 | 5999 |
 
  
[Answer]
# JavaScript (ES6), ~~75 73~~ 71 bytes
*Saved 1 byte thanks to @Shaggy*  
*Saved 2 bytes thanks to @Neil*
```
s=>/1|7|(2[^07]|3[578]|42|599?|50|6[789]|8[0578]|9[679]|.)./.exec(s)[0]
```
[Try it online!](https://tio.run/##ddDNTsMwDAfwO0/RW9oDbWLHSXwYPEhUpKl0CDStiKJph7x7yYZA@QAfIsX9xXL/b/vzfp0@Xt8/70/L87wddtu6exhUsKEF/yTtGNCTdWPQEIj5MZAMxlvHY3Be3r6wNzZe@64f@vkyT@3aeTlu03Jal@PcH5eX9tAKAgajGYhFc62ua4ahIbgrHEYFbCyo1GHlFJH8Fk3iVDUOFShkLTKGWDqbzvp1tmRAGpSKJ6bbxXsFpYxxAVvIoKz2y//iZz@qofkTmtI5kgSotclydvVAtkzkDKJOYexWSWvHjkghsYtvxD9Jw61EsWBslTDmEotEDq/d7Qs "JavaScript (Node.js) – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~28~~ ~~25~~ 24 bytes
```
η•Aó¾«¸tEΓ∞ζ∊u½d•.¥¤ØªKн
```
[Try it online!](https://tio.run/##HYstDsJAEIX9nKJBA9md2dnuGBoEilNAQKAQ/CRNEMgGi0EhCBiQJIApYusbztCLLAufeHl5P/PFaDybhs06z1pJp5e0sjzUz2Z76ld3//Y3/1oO6n1THOtHU@xWvpzErusv/lwd/HX4KUM7MApaI8gCFA2KTVGDZlZApFGTGEgB2aDWUQlQKRZBSRHoN7IKHCtGMsYKSCrMzhIZ0MaJY9bE4mIK@AfiOcJf "05AB1E – Try It Online")
```
η                            # prefixes of the input
 •Aó¾«¸tEΓ∞ζ∊u½d•            # compressed integer 211112166111113621489811655218129
                 .¥          # undelta: [0, 2, 3, 4, 5, 6, 8, 9, 15, 21, 22, 23, 24, 25, 26, 29, 35, 37, 38, 42, 50, 59, 67, 68, 69, 75, 80, 85, 87, 88, 96, 97, 99, 108]
                   ¤         # last element of that list: 108
                    Ø        # nth prime: 599
                     ª       # append it to the list
                      K      # remove all those values from the list of prefixes
                       н     # get the first prefix left
```
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), 60 bytes
```
!`^(2[1-69]?|3[578]?|42?|599?|50?|6[789]?|8[578]?|9[679]?)?.
```
[Try it online!](https://tio.run/##LU4xDsJADNvzChiQYCi6OJfcZaEjj6hAZWBgYUCM/XvJXckQ27Fk5/P8vt6P9XC8zut@vh8x8WB@GxeZtNTAjHFR91hpXGwqtZn1b/pkJfRpPK@rwmHZob4bLjsFSUi4FXA7CIhVU6NMIgwWz90QKg0LQTOYY0vTgYSUohte0C@JZEsIILGNGlVNCsnZenMo8uKq1UR6QyjiXL2qsqjXMLc30KdHAxRNMd1q9Ac "Retina 0.8.2 – Try It Online")
[Answer]
# [Bash](https://www.gnu.org/software/bash/), 328 ~~341~~ ~~410~~ bytes
Not very competitive for a golfing score, but each of these helps my bash skills!
Saved 13 bytes by eliminating handling 2 byte run-length values; storing only 3 bytes adds 8 bytes to T, but makes the case statement much simpler.
Saved 69 bytes by changing approach from bash expansion to storing deltas. The previous TIO link is at the bottom of my answer.
```
T='16D73r423112r62r72r6F1224r53-03511322rZr32r9L1611-01Fr9BrD2112V12-025r9-029r8-0202rB2r7-0308-162121E5r832-02082r72Cr52-3UR132'
A(){
V=$[V+36#$1]
S="$S $V"
}
R(){
read -n$1 d
}
while read -n1 c;do
case $c in -)R 3;A $d;;r)R 1;for((i=1;$i<=36#$d;i++)){ A 1;};;*)A $c;;esac;done<<<$T
for s in $S;do [[ $1 =~ ^$s ]]&&echo $s;done
```
[Try it online!](https://tio.run/##LZBBa8JAEIXv@ysGu6ipLGRmszFhk4PWeupJbQ4VC2mSYqAoTA49iP3r6Wzp5TG8mffNMB/1cB7HQznDdLO0nJBFJE6Jl6JbJErYWRNbh2iJ@I0tcf6CKaKJccv5mjckiQrJxOQ4F805E42J10KRaJwZTAkJnx1nNszFWeA/sSNjX3cCnqnVPLqpqtTHamHTB40ntS8neg@6mqi72oUud3UL5qIRWrG@z/1XB/8eQuPbq2rqoQPdQH8BE@3A@hXo1nuWGv3nlefzvkSv@6IMO1rfLxZRdIOVdO/eP0Yy3njfDXWgXbqiKPRBSQ6GgNR7ceF4BLmg/IF3PcDpNJ12zfkKevhLjONILgkPcYn9BQ "Bash – Try It Online")
* Sorted the list of prefixes numerically
* T is a "sort of" run-length encoded string showing the delta from the previous value. Each character is one of the following:
	+ A base36 value showing the increase from the previous value
	+ 'r': indicates that the next character indicates the the base36 encoded number of repeated delta values of 1.
	+ '-': indicates that the next 3 characters are the next delta value
The string T="16D73r42 [...] -3UR132" following the rules above becomes a list of deltas: "1 6 D 7 3 r4 2 [...] 4995 1 3 2"
Could save 2-3 more bytes by using a higher radix than 36 (like 62-64) but Excel only natively supports up to 36, and that's what used to do the list of deltas and their conversions.
* On running, T is parsed and expanded into the string S used for the comparison of the phone number given in command line argument 1.
Expanding T, S becomes: "1 7 20 27 30 31 32 33 34 36 [...] 5993 5994 5997 5999"
~~[Try it online!](https://tio.run/##VY/BboMwEETv/opVhMJlibGNwW5Ff4IcckzaECUSgiq0EZLlb6e75lRLNjv2m9HweZnv63pscyV0KFEFhRoN1ugiBl0Wtadvg57faCh8pCVMCGVR0YvFhkgShNXoo6gSmKLQEGDYISxpz5iLYEsPNqg0eh8MVpRBzpoz64gNsUGzDR1fcYpNkkoUmtIa4UqCXBQEcF9qYjdzvdntPzv/TBSelSWVajDHpR2lKnZ6F3NxanNtPRhX0XbgrKJdgW9cLrr23L8uA/Rf9wmycJSykIdDPIvb9IQFHiNkp/frBF2bhU7uIFtgJyHS1dgnZk5Mx0y/fD8hU/AG2fwhr/1Ljr/DsN9v2TMNy@MnOdd11bbSStFp/gA "Bash – Try It Online")~~
[Answer]
# [Python 3](https://docs.python.org/3/), ~~120~~ 78 bytes
```
f=lambda n:{n//10+3}-{*b'	

 &()-5>FGHSXZ[cdf',602}and f(n//10)or n
```
[Try it online!](https://tio.run/##HY5LT8MwEIQPCMqjP4Cn5BNxYEPtXa@TrUSPwJ0LglNKZFGJulXlC4ry21PTOYxGo/mk2f6ln02kcQzPv@162bUqzvs4m1nzSEPVPyyL46OTyenZxfTy6vrm9k7d67Lixcvr2/vH59d3FwrwBoc2diroA1dudiqOIXtSq6gYBb0TZAHKAcXXaMEyGyCyaEkc1IDs0NrsBGgMi6DUCPQ/8gYaNozknBeQWpgbT@TAukYaZkssTW4BD4IMZ/H8XKntbhWTLvpBVQvVD8VTfrVuk04QdCrLctwD "Python 3 – Try It Online")
Contains some unprintables: 
```
00000000: 663d 6c61 6d62 6461 206e 3a7b 6e2f 2f31  f=lambda n:{n//1
00000010: 302b 337d 2d7b 2a62 2705 0306 0708 090b  0+3}-{*b'.......
00000020: 0c18 191a 1b1c 1d20 2628 292d 353e 4647  ....... &()-5>FG
00000030: 4853 585a 5b63 6466 272c 3630 327d 616e  HSXZ[cdf',602}an
00000040: 6420 6628 6e2f 2f31 3029 6f72 206e       d f(n//10)or n
```
Somewhat ungolfed (earlier) version:
```
f=lambda n:{n/10}-{0,2,3,4,5,6,8,9,21,22,23,24,25,26,29,35,37,38,42,50,59,599,67,68,69,80,85,87,88,96,97,99}and f(n/10)or n
```
[Try it online!](https://tio.run/##FY/BagMxDETv/QrfsguT1pYsWwq0/7IlLA00Tgi@lGW/fSsLMQgh5o2ef/3n0eg41s/f5f59XUK7bO0jxf28RRAYGYIChYESiEAMyiABFZCBBVzBikyQCDFvQ6koimLQCBVohbpDgVWY7Uu7hnUalPnxCu1YXXu4tSBkVLKRu7APZKU6NIkbMydKbBnV2ZlScvUkMTqOrHrScVQGLgpxzg63aiJamDNSVlORxGLqW39k1MjqJZe3EJ6vW@unbQ/nr7Dtp3cPdV/61LFOfZ6Pfw "Python 2 – Try It Online")
[Answer]
# [Perl 5](https://www.perl.org/) (-p), 44 bytes
```
$\=chop until/^599$/+vec"\x7D\x03\xE0\x27\x68\x04\x04\x08\x38\x00\xA1\x01\x0B",$_,1
```
[Try it online!](https://tio.run/##K0gtyjH9/18lxjY5I79AoTSvJDNHP87U0lJFX7ssNVkppsLcJabCwDimwtUgpsLIPKbCzALIN4FiINsYxAfKORoCaRB2UtJRidcx/P/f0NTU4F9@QUlmfl7xf90CAA "Perl 5 – Try It Online")
Both TIO and SO have troubles with unprintable chars, so the program is shown with escape sequences. Here's a hexdump of the actual 44 bytes:
```
0000000: 245c 3d63 686f 7020 756e 7469 6c2f 5e35  $\=chop until/^5
0000010: 3939 242f 2b76 6563 227d 03e0 2768 0404  99$/+vec"}..'h..
0000020: 0838 00a1 010b 222c 245f 2c31            .8....",$_,1
```
[Answer]
# [PHP](https://php.net/), 219 bytes
I feel like there's a lot of room for improving the regex - golfed it as far as I can go but I'll bet it can be way shorter....
```
preg_match('/(1|7|2(0|1[12368]|[2346].|5[^9]|7|9[01789])|3[578]?.|42?[013]|5([1-8]|0.|99?[3479])|6([0-6]|7[^1]|8[^4]|9[012])|8(0[08]|[1246]|5[02356]|7[05-9]|8[0-368])|9([0-58]|6[^9]|7[^8]|9[1-8]))/',$argn,$r);echo$r[0];
```
[Try it online!](https://tio.run/##JY7NasMwEIRfxhAJIme1@rFECr71JRa5hBDiHBoLN8d99qqr9DKH3Zn5pq61fcxVtO63@9f35XVd1eGkLE@MCtiSRRdTYULnYxk50JKLPDOBnVIumh2FKZV5ZI@zHF3hoMgaycDIOc/k/NR9URGYKFlabOFEiy/vFpRfUkDQKRaFIhBAF95eCCZ3N5g@Q3PuLUGs8X8ILanXdJ7Wp8NxuOz353HY9fl2XbdhJyjn1jB4tFbUtd@tvh7b86eZzz8 "PHP – Try It Online")
[Answer]
# Java 8, 84 bytes
```
s->s.replaceAll("(1|7|(2[^07]|3[578]|42|599?|50|6[789]|8[0578]|9[679]|.).).*","$1");
```
Port of [*@Arnauld*'s JavaScript regex](https://codegolf.stackexchange.com/a/193109/52210), so make sure to upvote him!
[Try it online.](https://tio.run/##ZVHLTsMwELz3K6yIQ0JpFD82tqkAcYdeeoyCFNKAUtI0qt1KCPfbwzYEKYa1ZHlm1zvj9bY4FYvt5qMvm8IY8lzU7deMkLq11eGtKCuyukBC1vZQt@@kDMeDiZbIn2e4GVvYuiQr0pK73izuTXyougbvPjZNGITUSRey7CWRueMZSJU7wRxo/eAgcWkmlc6dypIho7NUIowjXNfBTXBFg2jZLy8y3fG1QZlR7bSvN2SHbkdDWU6K6MeqrYwNgWmWCs1APw1OR5ojyXQqGfVoCpB4ZZwyyrWYcnIKGAhGKe7c68OSBB/GtGS@6p/uqQcVJMC4EKlvVUsNoFLOhW9VKK0AKAetsMKXH2LKoBsM@PdZw/iGimaPv1m33dGO41t/Glvt4v3Rxh1O1jZtOKTnwS0J5m1c/sIgGtue@28)
**Explanation:**
```
s->                    // Method with String as both parameter and return-type
  s.replaceAll("(1|7|(2[^07]|3[578]|42|599?|50|6[789]|8[0578]|9[679]|.).).*",
                       //  Replace this regex-match
               "$1");  //  With this replacement
```
*Regex explanation:*
```
(1|7|(2[^07]|3[578]|42|599?|50|6[789]|8[0578]|9[679]|.).).*  // MATCH:
 1                                                           //  a 1
  |7                                                         //  or a 7
    |(                                                )      //  or:
      2[^07]                                                 //   a 2 not followed 0 nor 7
            |3[578]                                          //   or a 3 followed by 5, 7, or 8
                   |42                                       //   or a 42
                      |599?                                  //   or a 59 or a 599
                           |50                               //   or a 50
                              |6[789]                        //   or a 6 followed by 7, 8, or 9
                                     |8[0578]                //   or an 8 followed by 0, 5, 7, or 8
                                             |9[679]         //   or a 9 followed by 6, 7, or 9
                                                    |.       //   or any single digit
                                                       .     //  followed by any single digit
(                                                       )    //  All captured in capture group 1
                                                         .*  //  With 0 or more digits following
$1                                                           // REPLACEMENT:
$1                                                           //  The match of capture group 1,
                                                             //  (effectively removing the
                                                             //   remaining digits of `.*`)
```
[Answer]
# [Python 3](https://docs.python.org/3/), 96 bytes
```
lambda s:re.sub('(1|7|(2[^07]|3[578]|42|599?|50|6[789]|8[0578]|9[679]|.).).*',r'\1',s)
import re
```
[Try it online!](https://tio.run/##HU7RasMwDHzXV@QtzijFkizbKpR9iJtBy1IaaNPgpIyB/z3zeoLjdDrBzb/r7Tnxdj2etvv5cfk@N8shD/vldTGtwRKKofRlQ184SYh9cVRE9bOILT6FqH2Jyb4vmnyo676r89HucnvCdrd0MD7mZ16bPGw/t/E@NHiY8zit5mrGaX6tpuu6TUjJOyVR4CpIfSAEFLHAjISsDgKQOEKszEDW1hqkgYD/Q95CFCvEznkFDSoSPbMDdFGjCLJorC7QG1CfK@QP "Python 3 – Try It Online")
[Port of Arnauld's JavaScript answer.](https://codegolf.stackexchange.com/a/193109/87681)
[Answer]
# [Scala](http://www.scala-lang.org/), 411 402 330 bytes
This is assuming that the argument only contains digits.
```
"(1|(2(0|(1[12368])|([2346]\\d)|(5[^9])|7|(9[^2-6])))|(3(([0-4])|([57]\\d)|6|(8[^48])|9))|(4([^2]|(2[013])))|(5((0\\d)|[^09]|(9([^9]|(9[3479])))))|(6([0-6]|(7[^1])|(8[^4])|(9[0-2])))|7|(8((0[08])|[1246]|(5[02356])|(7[05-9])|(8[^4579])))|(9([0-58]|(6[^9])|(7[^8])|(9[^079]))))(.*)".r.unapplySeq(args(0)).foreach(l=>println(l.head))
```
[Try it online!](https://tio.run/##RVHNboMwDL73KaKeyCRQCEkgSN20B9ip2sklUgr0Z2K0o2xStfXZWRLT7WDJjr8fx77UtrPTdNq@tfVIXuyxJ98LQpp2R95dEdlhfynJ8zDYK6zH4djvK1qS1/44klVAEvJlO9KvllH6Ez2V3AXzSQopz1RRUV8Az4SqNpsmVBKMdu@5zzUYHquK0tDJXHg4i8VMlDnSlK8KMMIrakQLDzC8Cr7AUn6XkSjDAhMM0wGiA3zOIRO59nhkKHRVoZmDSdHeG2KmXRf1w9jF7ADMz@O@KpAqgfFMKuTkwGSs/5UkOv7NwmJZBJbChaB1cTc0bJ4wSh7oMhnCsvvks7fnc3ddtx/hOBGjNNmdhtbWh8iuHs/uRmPnDpccWttQ6li3xW2aJncOYbd18ws "Scala – Try It Online")
]