Regular Expressions 101

Save & Share

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

/
/
gm

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)\n" r"(?# GUILDS: )\n" r"(?'assassins'(d(octo)?r|professor))\n" r"(?'priests'((?(?=blessed|venerable|holy)(blessed|venerable|holy)( (brother|sister|father|mother))?|(brother|sister|father|mother))|(mostly )?reverend|blessed|beatus|saint|high priest(ess)?|(his|her|it'?s) eminence|minister|outcast))\n" r"(?'thieves'(crafty|crooked|dastardly|dishonest|dodgy|elusive|evasive|furtive|greased|honest|latent|((light|quick)[-]|butter)?finger(ed|s)|quiet|shady|shifty|silent|slick|sly|tricky))\n" r"(?'witches'(?# duplicates: mother, old, mistress, sister)((?# goodie, goody)good(y|ie)|gammer|gra(mma|nny)|(?# mss, mee)m[se]{2}|(?# nanny, nanna)nann[ay]|aunty|biddy|black|mama|wee|wicked|young))\n" r"(?'wizards'(fat|stuffed|overfed|gimlet[-]eyed|robust|bearded|burly|plump|rotund|thin|tiny|mystic|obscure|complex|learned|potent|wise|grumpy|cryptic|dark|scholarly|grey[-]?(haired|beard)|adroit|dire|maven|quantum|savant|unseen|(arch)?(master|mistress|mage)))\n" r"(?# COUNCIL: )\n" r"(?'council_am'(dame|lady|lord|sir)) \n" r"(?'court_positive'the (amazing|civic[-]minded|elegant|eloquent|(helpful|upstanding(?= citizen))( citizen)?|stylish|utterly fluffy|wonderful))\n" r"(?'court_punishment'(appallingly filthy|corpse looter|dull|feebleminded|i (promise i won't do it again|got punished( and all i got was this lousy title)?)|insignificant|lying|malingering|naughty spawn|necrokleptomaniac|offensive|pillock|pointless|repentant|reprobate|shopkeeper murderer|silly spammy git|sitting in the corner|smelly|tantrum thrower|too stupid to live|vagrant|(very ){1,2}sorry|waste of space|whinging))\n" r"(?'council_djb'(?# duplicates: feebleminded, corpse looter, cowardly)(sultana?|(shai|sitt) (al[-](khasa|ri'asa)|ishqu?araya|a'daha)|nawab|qasar|mazrat|effendi|ya'uq|mutasharid|ishqu?araya|naughty spawn|kill stealer|idiotic|offensive|corpse looter|cat hating|heathen|foreign dog|infidel|shopkeeper murderer|destitute|parasitic|hated|cowardly|criminal|felon))\n" r"(?# ACHIEVEMENTS: )\n" r"(?'achievements_thieves'(ruinous|fingers))\n" r"(?'achievements_warriors'(centurion|chef|head(master|mistress)|impaler|pulveriser))\n" r"(?'achievements_witches'(destined|nasty|terrible))\n" r"(?'achievements_fools'pious)\n" r"(?'achievements_wizards'(erratic mechanic|mysterious|arcana))\n" r"(?'achievements_priests'(templar|healer|saintly))\n" r"(?'achievements_assassins'(lethal|venomous))\n" r"(?'achievements_all'(?# axe-master, shieldmaster/mistress, staffmaster/mistress)((sword|shield|staff|axe[-])(master|mistress)|antiquated|archaic|old( (wo)?man)?|bloodthirsty|bruiser|champion|competent|contender|crimewave|crusher|cultured|cutthroat|deckhand|decrepit|diplomatic|duelist|elementalist|energetic|exterminator|festive|filthy|flatulent|fossilized|gifted|golden|knifey|legendary|literate|masterful|medical|miner|multilingual|[nm]urse|mythical|nimble|obsolete|opulent|paranoid|perverse|prehistoric|rock[-]hard|rouge|senile|captain|stormrider|unburiable|unexpected|unlucky|unstoppable|venerable|versatile|virtuoso|wealthy))\n" r"(?'quest_points'(well travelled|persistent))\n" r"(?# MISC: )\n" r"(?'general'm([sx]|rs?|iss))\n" r"(?'genua'(?# m, monsieur, mlle, mademoiselle, mme, madame)m(?=(\Z|$| |me|lle|onsieur|adame|ademoiselle))(me|lle|onsieur|adame|ademoiselle)?)\n" r"(?'ghosts'(lonely|mournful|scary|spooky|wandering))\n" r"(?'musketeers'(cheating|cowardly))\n" r"(?'debaters'(diplomatic|uncreative)))(?#\n" r" TITLE REGEX: \n" r")^(?<title>(?P>assassins)|(?P>priests)|(?P>thieves)|(?P>witches)|(?P>wizards)|(?P>council_am)|(?P>court_positive)|(?P>court_punishment)|(?P>council_djb)|(?P>achievements_thieves)|(?P>achievements_warriors)|(?P>achievements_witches)|(?P>achievements_fools)|(?P>achievements_wizards)|(?P>achievements_priests)|(?P>achievements_assassins)|(?P>achievements_all)|(?P>quest_points)|(?P>general)|(?P>genua)|(?P>ghosts)|(?P>musketeers)|(?P>debaters))") test_str = ("GENERAL\n" "miss\n" "mr\n" "mrs\n" "ms\n" "mx\n" "GHOSTS\n" "lonely\n" "mournful\n" "scary\n" "spooky\n" "wandering\n" "MUSKATEERS\n" "cheating\n" "cowardly\n" "ASSASSINS\n" "doctor\n" "dr\n" "professor\n" "PRIESTS\n" "brother\n" "sister\n" "mostly reverend\n" "reverend \n" "blessed \n" "blessed father \n" "blessed mother \n" "blessed brother \n" "blessed sister \n" "venerable \n" "venerable brother \n" "venerable sister \n" "venerable father \n" "venerable mother \n" "holy \n" "holy brother \n" "holy sister \n" "beatus \n" "saint \n" "high priest\n" "high priestess\n" "his eminence\n" "her eminence\n" "it's eminence\n" "its eminence\n" "minister\n" "THIEVES\n" "butterfingers\n" "crafty\n" "crooked\n" "dastardly\n" "dishonest\n" "dodgy\n" "elusive\n" "evasive\n" "fingers\n" "furtive\n" "greased\n" "honest\n" "latent\n" "light-fingered\n" "quick-fingered\n" "quiet\n" "shady\n" "shifty\n" "silent\n" "slick\n" "sly\n" "tricky\n" "WITCHES\n" "aunty\n" "biddy\n" "black\n" "gammer\n" "goodie\n" "goody\n" "gramma\n" "granny\n" "mama\n" "mistress\n" "mother\n" "mss\n" "nanna\n" "nanny\n" "old\n" "sister\n" "wee\n" "wicked\n" "young\n" "WIZARDS\n" "fat\n" "stuffed\n" "overfed\n" "gimlet-eyed\n" "robust\n" "bearded\n" "burly\n" "plump\n" "rotund\n" "thin\n" "tiny\n" "mystic\n" "obscure \n" "complex\n" "learned \n" "potent\n" "wise\n" "grumpy \n" "cryptic\n" "dark\n" "scholarly\n" "grey-haired\n" "greybeard\n" "master\n" "mistress \n" "adroit\n" "dire\n" "maven \n" "quantum\n" "savant\n" "unseen \n" "archmaster\n" "mistress\n" "archmage \n" "COUNCIL AM\n" "dame \n" "lady \n" "lord \n" "sir \n" "COURT: POSITIVE\n" "the amazing\n" "the civic-minded\n" "the elegant\n" "the eloquent\n" "the helpful\n" "the helpful citizen\n" "the stylish\n" "the upstanding citizen\n" "the utterly fluffy\n" "the wonderful\n" "COURT: PUNISHMENT\n" "appallingly filthy\n" "corpse looter\n" "dull\n" "feebleminded\n" "i got punished\n" "i got punished and all i got was this lousy title\n" "i promise i won't do it again\n" "insignificant\n" "lying\n" "malingering\n" "naughty spawn\n" "necrokleptomaniac\n" "offensive\n" "pillock\n" "pointless\n" "repentant\n" "reprobate\n" "shopkeeper murderer\n" "silly spammy git\n" "sitting in the corner\n" "smelly\n" "tantrum thrower\n" "too stupid to live\n" "vagrant\n" "very sorry\n" "very very sorry\n" "waste of space\n" "whinging\n" "COUNCIL: DJB\n" "sultan \n" "sultana \n" "shai al-khasa \n" "sitt al-khasa \n" "shai al-ri'asa \n" "sitt al-ri'asa \n" "shai ishquaraya \n" "sitt ishquaraya \n" "shai a'daha \n" "sitt a'daha \n" "nawab \n" "qasar \n" "mazrat \n" "effendi \n" "ya'uq \n" "mutasharid \n" "ishqaraya \n" "naughty spawn \n" "kill stealer \n" "feebleminded \n" "idiotic \n" "offensive \n" "corpse looter \n" "cat hating \n" "heathen \n" "foreign dog \n" "infidel \n" "shopkeeper murderer \n" "destitute \n" "parasitic \n" "hated \n" "cowardly \n" "criminal \n" "felon \n" "GENUA\n" "m \n" "monsieur\n" "mlle \n" "mademoiselle\n" "mme \n" "madame\n" "QUEST: POINTS\n" "well travelled\n" "persistent\n" "DEBATERS:\n" "diplomatic \n" "uncreative \n" "THIEVES\n" "fingers\n" "ruinous\n" "WARRIORS\n" "centurion\n" "chef\n" "headmaster\n" "headmistress\n" "impaler\n" "pulveriser\n" "WITCHES\n" "destined\n" "nasty\n" "terrible\n" "FOOLS\n" "pious\n" "WIZARDS\n" "erratic mechanic\n" "arcana\n" "mysterious\n" "PRIESTS\n" "healer\n" "saintly\n" "templar\n" "outcast\n" "ASSASSINS\n" "lethal\n" "venomous\n" "ALL\n" "antiquated\n" "archaic\n" "axe-master\n" "bloodthirsty\n" "bruiser\n" "captain\n" "champion\n" "competent\n" "contender\n" "crimewave\n" "crusher\n" "cultured\n" "cutthroat\n" "deckhand\n" "decrepit\n" "diplomatic\n" "duelist\n" "elementalist\n" "energetic\n" "exterminator\n" "festive\n" "filthy\n" "flatulent\n" "fossilized\n" "gifted\n" "golden\n" "knifey\n" "legendary\n" "literate\n" "masterful\n" "medical\n" "miner\n" "multilingual\n" "murse\n" "mythical\n" "nimble\n" "nurse\n" "obsolete\n" "old\n" "old man\n" "old woman\n" "opulent\n" "paranoid\n" "perverse\n" "prehistoric\n" "rock-hard\n" "rouge\n" "senile\n" "shieldmaster\n" "shieldmistress\n" "staffmaster\n" "staffmistress\n" "stormrider\n" "swordmaster\n" "swordmistress\n" "unburiable\n" "unexpected\n" "unlucky\n" "unstoppable\n" "venerable\n" "versatile\n" "virtuoso\n" "wealthy") matches = re.finditer(regex, test_str, re.MULTILINE) 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