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

$re = '/(?(DEFINE) (?# GUILDS: ) (?\'assassins\'(d(octo)?r|professor)) (?\'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)) (?\'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)) (?\'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)) (?\'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))) (?# COUNCIL: ) (?\'council_am\'(dame|lady|lord|sir)) (?\'court_positive\'the (amazing|civic[-]minded|elegant|eloquent|(helpful|upstanding(?= citizen))( citizen)?|stylish|utterly fluffy|wonderful)) (?\'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)) (?\'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)) (?# ACHIEVEMENTS: ) (?\'achievements_thieves\'(ruinous|fingers)) (?\'achievements_warriors\'(centurion|chef|head(master|mistress)|impaler|pulveriser)) (?\'achievements_witches\'(destined|nasty|terrible)) (?\'achievements_fools\'pious) (?\'achievements_wizards\'(erratic mechanic|mysterious|arcana)) (?\'achievements_priests\'(templar|healer|saintly)) (?\'achievements_assassins\'(lethal|venomous)) (?\'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)) (?\'quest_points\'(well travelled|persistent)) (?# MISC: ) (?\'general\'m([sx]|rs?|iss)) (?\'genua\'(?# m, monsieur, mlle, mademoiselle, mme, madame)m(?=(\Z|$| |me|lle|onsieur|adame|ademoiselle))(me|lle|onsieur|adame|ademoiselle)?) (?\'ghosts\'(lonely|mournful|scary|spooky|wandering)) (?\'musketeers\'(cheating|cowardly)) (?\'debaters\'(diplomatic|uncreative)))(?# TITLE REGEX: )^(?<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))/m'; $str = 'GENERAL miss mr mrs ms mx GHOSTS lonely mournful scary spooky wandering MUSKATEERS cheating cowardly ASSASSINS doctor dr professor PRIESTS brother sister mostly reverend reverend blessed blessed father blessed mother blessed brother blessed sister venerable venerable brother venerable sister venerable father venerable mother holy holy brother holy sister beatus saint high priest high priestess his eminence her eminence it\'s eminence its eminence minister THIEVES butterfingers crafty crooked dastardly dishonest dodgy elusive evasive fingers furtive greased honest latent light-fingered quick-fingered quiet shady shifty silent slick sly tricky WITCHES aunty biddy black gammer goodie goody gramma granny mama mistress mother mss nanna nanny old sister wee wicked young 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 greybeard master mistress adroit dire maven quantum savant unseen archmaster mistress archmage COUNCIL AM dame lady lord sir COURT: POSITIVE the amazing the civic-minded the elegant the eloquent the helpful the helpful citizen the stylish the upstanding citizen the utterly fluffy the wonderful COURT: PUNISHMENT appallingly filthy corpse looter dull feebleminded i got punished i got punished and all i got was this lousy title i promise i won\'t do it again 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 sorry very very sorry waste of space whinging COUNCIL: DJB sultan sultana shai al-khasa sitt al-khasa shai al-ri\'asa sitt al-ri\'asa shai ishquaraya sitt ishquaraya shai a\'daha sitt a\'daha nawab qasar mazrat effendi ya\'uq mutasharid ishqaraya naughty spawn kill stealer feebleminded idiotic offensive corpse looter cat hating heathen foreign dog infidel shopkeeper murderer destitute parasitic hated cowardly criminal felon GENUA m monsieur mlle mademoiselle mme madame QUEST: POINTS well travelled persistent DEBATERS: diplomatic uncreative THIEVES fingers ruinous WARRIORS centurion chef headmaster headmistress impaler pulveriser WITCHES destined nasty terrible FOOLS pious WIZARDS erratic mechanic arcana mysterious PRIESTS healer saintly templar outcast ASSASSINS lethal venomous ALL antiquated archaic axe-master bloodthirsty bruiser captain 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 murse mythical nimble nurse obsolete old old man old woman opulent paranoid perverse prehistoric rock-hard rouge senile shieldmaster shieldmistress staffmaster staffmistress stormrider swordmaster swordmistress unburiable unexpected unlucky unstoppable venerable versatile virtuoso wealthy'; preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0); // Print the entire match result var_dump($matches);

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 PHP, please visit: http://php.net/manual/en/ref.pcre.php