# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"""
(?(DEFINE)
(?# Definitions )
(?<valid_nameChars>[\p{L}\p{Nl}])
(?<valid_nonNameChars>[^\p{L}\p{Nl}\p{Zs}])
(?<valid_startFirstName>(?![a-z])[\p{L}'])
(?<valid_upperChar>(?![a-z])\p{L})
(?<valid_nameSeparatorsSoft>[\p{Pd}'])
(?<valid_nameSeparatorsHard>\p{Zs})
(?<valid_nameSeparators>(?&valid_nameSeparatorsSoft)|(?&valid_nameSeparatorsHard))
(?# Invalid combinations )
(?<invalid_startChar>^[\p{Zs}a-z])
(?<invalid_endChar>.*[^\p{L}\p{Nl}.\p{C}]$)
(?<invalid_unaccompaniedSymbol>.*(?&valid_nameSeparatorsHard)(?&valid_nonNameChars)(?&valid_nameSeparatorsHard))
(?<invalid_overTwoUpper>(?:(?&valid_nameChars)*\p{Lu}){3})
(?<invalid>(?&invalid_startChar)|(?&invalid_endChar)|(?&invalid_unaccompaniedSymbol)|(?&invalid_overTwoUpper))
(?# Valid combinations )
(?<valid_name>(?:(?:(?&valid_nameChars)|(?&valid_nameSeparatorsSoft))*(?&valid_nameChars)+(?:(?&valid_nameChars)|(?&valid_nameSeparatorsSoft))*)+\.?)
(?<valid_firstName>(?&valid_startFirstName)(?:\.|(?&valid_name)*))
(?<valid_multipleName>(?&valid_firstName)(?=.*(?&valid_nameSeparators)(?&valid_upperChar))(?:(?&valid_nameSeparatorsHard)(?&valid_name))+)
(?<valid>(?&valid_multipleName)|(?&valid_firstName))
)
^(?!(?&invalid))(?&valid)$
"""
test_str = ("== 1NcOrrect N4M3S ==\n"
"CAPITAL LETTER\n"
"AlTeRnAtE LeTtEr\n"
"Natalia maria\n"
"Natalia aria\n"
"Natalia orea\n"
"Maria dornelas\n"
"Samuel eto'\n"
"Miguel lasagna\n"
"Antony1 de Home Ap*ril\n"
"Ap*ril Willians\n"
"Antony_ de Home Apr+il\n"
"Ant_ony de Home Apr#il\n"
"Antony@ de Ho@me Apr^il\n"
"Maria Silva\n"
"Maria silva\n"
"maria Silva\n"
" Maria Silva\n"
"Maria Silva \n"
"Maria / Silva\n"
"Maria . Silva\n"
"John W8\n\n"
"==Correct Names==\n"
"Urxan Əbűlhəsənzadə\n"
"İsmət Jafarov\n"
"Şükür Hagverdiyev\n"
"Űmid Abdurrahimov\n"
"Ġerardo Seralta\n"
"Ċikku Paris\n"
"Hind ibn Sheik\n"
"Colop-U-Uichikin\n"
"Lażżru Role\n"
"Alaksiej Taraškievič\n"
"Petruso Husoǔski\n"
"Sumu-la-El\n"
"Valeh ßlÿsgÿroğlu\n"
"'Arab al-Rashayida\n"
"Tariq al-Hashimi\n"
"Nabeeh el-Mady\n"
"Tariq Al-Hashimi\n"
"Brian O'Conner\n"
"Maria da Silva\n"
"Maria Silva\n"
"Maria G. Silva\n"
"Maria McDuffy\n"
"Getúlio Dornelles Vargas\n"
"Maria das Flores\n"
"John Smith\n"
"John D'Largy\n"
"John Doe-Smith\n"
"John Doe Smith\n"
"Hector Sausage-Hausen\n"
"Mathias d'Arras\n"
"Martin Luther King Jr.\n"
"Ai Wong\n"
"Chao Chang\n"
"Alzbeta Bara\n"
"Marcos Assunção\n"
"Maria da Silva e Silva\n"
"Juscelino Kubitschek de Oliveira\n"
"Maria da Costa e Silva\n"
"Samuel Eto'o\n"
"María Antonieta de las Nieves\n"
"Eugène\n"
"Antòny de Homé April\n"
"àntony de Home ùpril\n"
"Antony de Home Aprìl\n"
"Pierre de l'Estache\n"
"Pierre de L'Estoile\n"
"Akihito\n"
"Nadine Schröder\n"
"Anna A. Møller\n"
"D. Pedro I\n"
"Pope Benedict XVI\n"
"Marsibil Ragnarsdóttir\n"
"Natanaël Morel\n"
"Isaac De la Croix\n"
"Jean-Michel Bozonnet\n"
"Qutaibah Mu'tazz Abadi\n"
"Rushd Jawna' Kassab\n"
"Khaldun Abdul-Qahhar Sabbag\n"
"'Awad Bashshar Asker\n"
"Al B. Zellweger\n"
"Gunnleif Snæ-Ulfsson\n"
"Käre Toresson\n"
"Sorli Ærnmundsson\n"
"Arnkel Øystæinsson\n"
"Ástríður Dórey\n"
"Åsmund Kåresson\n"
"Yahatti-Il\n"
"Ipqu-Annunitum\n"
"Nabu-zar-adan\n"
"Eskopas Cañaverri\n"
"Botolph of Langchester\n"
"Aelfhun the Cantrell\n"
"Fraco di Natale\n"
"Fraco Di Natale\n"
"Iván de Luca\n"
"Iván De Luca\n"
"Man'nah\n"
"Atabala Aüamusalü\n"
"Ramiz Ağasəfalu\n"
"Dadaş Aghakhanov\n"
"Fÿrxad Mübarizlı\n"
"Vaclaǔ Šupa\n"
"Yakiv Volacič\n"
"Flor Van Vaerenbergh\n"
"Flor van Vaerenbergh\n"
"Edwin van der Sar\n"
"Husein Ekmečić\n"
"Álvaro Guimarães Alencar\n"
"Phone U Yaza Arkar\n"
"Seocan MacGhille\n"
"X'wat'e Tlekadugovy\n"
"Albert-Jan Bootsveld\n"
"Maurits-jan Kuipers op den Kollenstaart\n"
"Elco ter Hoek\n"
"Robbert te Poele\n"
"Aad ten Have\n"
"'Ehu Kali\n"
"Ho'opa'a Loni\n"
"Aukanai'i Mahi'ai\n"
"Kalman ben Tal El\n"
"Żytomir Roszkowski\n"
"K'awai\n\n"
"==EXTRA== only if possible, strange ones\n"
"Maol-Moire Mac'IlleBhuidh\n"
"Tòmas MacIlleChruim\n"
"Aindreas MacIllEathain\n"
"Eanruig MacGilleBhreac\n"
"Peadar MacGilleDhonaghart\n"
"Maolmhuire MacGill-Eain\n"
"Eanruig MacGilleBhreac\n"
"Wim van 't Plasman")
matches = re.finditer(regex, test_str, re.MULTILINE | re.VERBOSE)
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