Regular Expressions 101

Save & Share

  • Regex Version: ver. 1
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to Community Library

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

Sponsors
There are currently no sponsors. Become a sponsor today!
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression

/
/
gmx

Test String

Code Generator

Generated Code

# 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