# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(\+\d{1,4}( \d{1,4}){0,2})"
test_str = ("Åland Islands\n"
"+358 18\n"
"Afghanistan\n"
"+93\n"
"Albania\n"
"+355\n"
"Algeria\n"
"+213\n"
"American Samoa\n"
"+1 684\n"
"Andorra\n"
"+376\n"
"Angola\n"
"+244\n"
"Anguilla\n"
"+1 264\n"
"Antigua and Barbuda\n"
"+1 268\n"
"Argentina\n"
"+54\n"
"Armenia\n"
"+374\n"
"Aruba\n"
"+297\n"
"Ascension Island\n"
"+247\n"
"Australia\n"
"+61\n"
"Australian Antarctic Territory\n"
"+672 1\n"
"Austria\n"
"+43\n"
"Azerbaijan\n"
"+994\n"
"Bahamas\n"
"+1 242\n"
"Bahrain\n"
"+973\n"
"Bangladesh\n"
"+880\n"
"Barbados\n"
"+1 246\n"
"Belarus\n"
"+375\n"
"Belgium\n"
"+32\n"
"Belize\n"
"+501\n"
"Benin\n"
"+229\n"
"Bermuda\n"
"+1 441\n"
"Bhutan\n"
"+975\n"
"Bolivia\n"
"+591\n"
"Bosnia and Herzegovina\n"
"+387\n"
"Botswana\n"
"+267\n"
"Bouvet Island\n"
"+47\n"
"Brazil\n"
"+55\n"
"British Indian Ocean Territory\n"
"+246\n"
"British Virgin Islands\n"
"+1 284\n"
"Brunei\n"
"+673\n"
"Bulgaria\n"
"+359\n"
"Burkina Faso\n"
"+226\n"
"Burundi\n"
"+257\n"
"Cambodia\n"
"+855\n"
"Cameroon\n"
"+237\n"
"Canada\n"
"+1\n"
"Cape Verde\n"
"+238\n"
"Caribbean Netherlands\n"
"+599\n"
"Cayman Islands\n"
"+1 345\n"
"Central African Republic\n"
"+236\n"
"Chad\n"
"+235\n"
"Chile\n"
"+56\n"
"China\n"
"+86\n"
"Christmas Island\n"
"+61 89164\n"
"Cocos (Keeling) Islands\n"
"+61 89162\n"
"Colombia\n"
"+57\n"
"Comoros\n"
"+269\n"
"Cook Islands\n"
"+682\n"
"Costa Rica\n"
"+506\n"
"Croatia\n"
"+385\n"
"Cuba\n"
"+53\n"
"Curaçao\n"
"+599 9\n"
"Cyprus\n"
"+357\n"
"Czech Republic\n"
"+420\n"
"Democratic Republic of the Congo\n"
"+243\n"
"Denmark\n"
"+45\n"
"Djibouti\n"
"+253\n"
"Dominica\n"
"+1 767\n"
"Dominican Republic\n"
"+1 809\n"
"+1 829\n"
"+1 849\n"
"East Timor\n"
"+670\n"
"Ecuador\n"
"+593\n"
"Egypt\n"
"+20\n"
"El Salvador\n"
"+503\n"
"Equatorial Guinea\n"
"+240\n"
"Eritrea\n"
"+291\n"
"Estonia\n"
"+372\n"
"Eswatini\n"
"+268\n"
"Ethiopia\n"
"+251\n"
"Falkland Islands(Malvinas)\n"
"+500\n"
"Faroe Islands\n"
"+298\n"
"Fiji\n"
"+679\n"
"Finland\n"
"+358\n"
"France\n"
"+33\n"
"French Guiana\n"
"+594\n"
"French Polynesia\n"
"+689\n"
"French Southern and Antarctic Lands\n"
"+262\n"
"Gabon\n"
"+241\n"
"Gambia\n"
"+220\n"
"Georgia\n"
"+995\n"
"Germany\n"
"+49\n"
"Ghana\n"
"+233\n"
"Gibraltar\n"
"+350\n"
"Greece\n"
"+30\n"
"Greenland\n"
"+299\n"
"Grenada\n"
"+1 473\n"
"Guadeloupe\n"
"+590\n"
"Guam\n"
"+1 671\n"
"Guatemala\n"
"+502\n"
"Guernsey\n"
"+44 1481\n"
"+44 7781\n"
"+44 7839\n"
"+44 7911\n"
"Guinea\n"
"+224\n"
"Guinea-Bissau\n"
"+245\n"
"Guyana\n"
"+592\n"
"Haiti\n"
"+509\n"
"Heard Island and McDonald Islands\n"
"+1 672\n"
"Honduras\n"
"+504\n"
"Hong Kong\n"
"+852\n"
"Hungary\n"
"+36\n"
"Iceland\n"
"+354\n"
"India\n"
"+91\n"
"Indonesia\n"
"+62\n"
"Iran\n"
"+98\n"
"Iraq\n"
"+964\n"
"Ireland\n"
"+353\n"
"Isle of Man\n"
"+44 1624\n"
"+44 7524\n"
"+44 7624\n"
"+44 7924\n"
"Israel\n"
"+972\n"
"Italy\n"
"+39\n"
"Ivory Coast\n"
"+225\n"
"Jamaica\n"
"+1 876\n"
"Japan\n"
"+81\n"
"Jersey\n"
"+44 1534\n"
"Jordan\n"
"+962\n"
"Kazakhstan\n"
"+7 6\n"
"+7 7\n"
"Kenya\n"
"+254\n"
"Kiribati\n"
"+686\n"
"Kosovo\n"
"+383\n"
"Kuwait\n"
"+965\n"
"Kyrgyzstan\n"
"+996\n"
"Laos\n"
"+856\n"
"Latvia\n"
"+371\n"
"Lebanon\n"
"+961\n"
"Lesotho\n"
"+266\n"
"Liberia\n"
"+231\n"
"Libya\n"
"+218\n"
"Liechtenstein\n"
"+423\n"
"Lithuania\n"
"+370\n"
"Luxembourg\n"
"+352\n"
"Macao\n"
"+853\n"
"Madagascar\n"
"+261\n"
"Malawi\n"
"+265\n"
"Malaysia\n"
"+60\n"
"Maldives\n"
"+960\n"
"Mali\n"
"+223\n"
"Malta\n"
"+356\n"
"Marshall Islands\n"
"+692\n"
"Martinique\n"
"+596\n"
"Mauritania\n"
"+222\n"
"Mauritius\n"
"+230\n"
"Mayotte\n"
"+262 269\n"
"+262 639\n"
"Mexico\n"
"+52\n"
"Micronesia\n"
"+691\n"
"Moldova\n"
"+373\n"
"Monaco\n"
"+377\n"
"Mongolia\n"
"+976\n"
"Montenegro\n"
"+382\n"
"Montserrat\n"
"+1 664\n"
"Morocco\n"
"+212\n"
"Mozambique\n"
"+258\n"
"Myanmar\n"
"+95\n"
"Namibia\n"
"+264\n"
"Nauru\n"
"+674\n"
"Nepal\n"
"+977\n"
"Netherlands\n"
"+31\n"
"Netherlands Antilles\n"
"+599\n"
"New Caledonia\n"
"+687\n"
"New Zealand\n"
"+64\n"
"Nicaragua\n"
"+505\n"
"Niger\n"
"+227\n"
"Nigeria\n"
"+234\n"
"Niue\n"
"+683\n"
"Norfolk Island\n"
"+672 3\n"
"North Korea\n"
"+850\n"
"North Macedonia\n"
"+389\n"
"Northern Ireland\n"
"+44 28\n"
"Northern Mariana Islands\n"
"+1 670\n"
"Norway\n"
"+47\n"
"Oman\n"
"+968\n"
"Pakistan\n"
"+92\n"
"Palau\n"
"+680\n"
"Palestine\n"
"+970\n"
"Panama\n"
"+507\n"
"Papua New Guinea\n"
"+675\n"
"Paraguay\n"
"+595\n"
"Peru\n"
"+51\n"
"Philippines\n"
"+63\n"
"Pitcairn\n"
"+64\n"
"Poland\n"
"+48\n"
"Portugal\n"
"+351\n"
"Puerto Rico\n"
"+1 787\n"
"+1 939\n"
"Qatar\n"
"+974\n"
"Réunion\n"
"+262\n"
"Republic of the Congo\n"
"+242\n"
"Romania\n"
"+40\n"
"Russia\n"
"+7\n"
"Rwanda\n"
"+250\n"
"Saint Barthélemy\n"
"+590\n"
"Saint Helena\n"
"+290\n"
"Saint Kitts and Nevis\n"
"+1 869\n"
"Saint Lucia\n"
"+1 758\n"
"Saint Martin(French part)\n"
"+590\n"
"Saint Pierre and Miquelon\n"
"+508\n"
"Saint Vincent and the Grenadines\n"
"+1 784\n"
"Samoa\n"
"+685\n"
"San Marino\n"
"+378\n"
"Sao Tome and Principe\n"
"+239\n"
"Saudi Arabia\n"
"+966\n"
"Senegal\n"
"+221\n"
"Serbia\n"
"+381\n"
"Seychelles\n"
"+248\n"
"Sierra Leone\n"
"+232\n"
"Singapore\n"
"+65\n"
"Slovakia\n"
"+421\n"
"Slovenia\n"
"+386\n"
"Solomon Islands\n"
"+677\n"
"Somalia\n"
"+252\n"
"South Africa\n"
"+27\n"
"South Georgia and the South Sandwich Islands\n"
"+500\n"
"South Korea\n"
"+82\n"
"South Sudan\n"
"+211\n"
"Spain\n"
"+34\n"
"Sri Lanka\n"
"+94\n"
"Sudan\n"
"+249\n"
"Suriname\n"
"+597\n"
"Svalbard and Jan Mayen\n"
"+47 79\n"
"Sweden\n"
"+46\n"
"Switzerland\n"
"+41\n"
"Syria\n"
"+963\n"
"Taiwan\n"
"+886\n"
"Tajikistan\n"
"+992\n"
"Tanzania\n"
"+255\n"
"Thailand\n"
"+66\n"
"Togo\n"
"+228\n"
"Tokelau\n"
"+690\n"
"Tonga\n"
"+676\n"
"Trinidad and Tobago\n"
"+1 868\n"
"Tunisia\n"
"+216\n"
"Turkey\n"
"+90\n"
"Turkmenistan\n"
"+993\n"
"Turks and Caicos Islands\n"
"+1 649\n"
"Tuvalu\n"
"+688\n"
"Uganda\n"
"+256\n"
"Ukraine\n"
"+380\n"
"United Arab Emirates\n"
"+971\n"
"United Kingdom\n"
"+44\n"
"United States\n"
"+1\n"
"United States Minor Outlying Islands\n"
"+246\n"
"Uruguay\n"
"+598\n"
"Uzbekistan\n"
"+998\n"
"Vanuatu\n"
"+678\n"
"Vatican City\n"
"+379\n"
"Venezuela\n"
"+58\n"
"Vietnam\n"
"+84\n"
"Virgin Islands, U.S.\n"
"+1 340\n"
"Wallis and Futuna\n"
"+681\n"
"Western Sahara\n"
"+212\n"
"Yemen\n"
"+967\n"
"Zambia\n"
"+260\n"
"Zimbabwe\n"
"+263")
matches = re.finditer(regex, test_str, re.DOTALL)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html