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"^(.*)(\r?\n\1)+$" test_str = ("{\"??\", \"What?\"}\n" "{\"0mg\", \"Oh My God\"},\n" "{\"0wn\", \"owned\"},\n" "{\"10\\\\/3u\", \"love you\"},\n" "{\"13wt\", \"Loot\"},\n" "{\"2\", \"too\"},\n" "{\"31337\", \"elite\"},\n" "{\"4tw\", \"for the win\"},\n" "{\"A&D\", \"Attack and Defend\"},\n" "{\"AFAIK\", \"As Far As I Know\"},\n" "{\"AFK\", \"Away from keyboard\"},\n" "{\"ASL\", \"Age Sex Location\"},\n" "{\"ATM\", \"At The Moment\"},\n" "{\"BAF\", \"Bring A Friend\"},\n" "{\"BBIM\", \"Be Back In a Minute\"},\n" "{\"BBL\", \"Be Back Later\"},\n" "{\"BBS\", \"Be Back Soon\"},\n" "{\"BFG\", \"big fucking gun\"},\n" "{\"BRB\", \"Be Right Back\"},\n" "{\"BS\", \"Bullshit\"},\n" "{\"BTW\", \"By The Way\"},\n" "{\"Camper\", \"Ambush\"},\n" "{\"CFT\", \"Capture the flag\"},\n" "{\"CTF\", \"Capture the Flag\"},\n" "{\"CU\", \"See You\"},\n" "{\"d00d\", \"Dude\"},\n" "{\"DM\", \"Death Match\"},\n" "{\"DMG\", \"Damage\"},\n" "{\"dog8\", \"dog\"},\n" "{\"duma**\", \"idiot\"},\n" "{\"eet\", \"elite\"},\n" "{\"em\", \"Them\"},\n" "{\"exp\", \"experience points\"},\n" "{\"EXP\", \"Experience Points\"},\n" "{\"f00\", \"Fool\"},\n" "{\"FAQ\", \"Frequently Asked Questions\"},\n" "{\"FB\", \"Flash Bang grenade\"},\n" "{\"FFA\", \"Free For All\"},\n" "{\"FFS\", \"For Fuck's Sake\"},\n" "{\"FH\", \"Full Health\"},\n" "{\"Flak\", \"Debris\"},\n" "{\"Flame\", \"Nonstop insulting\"},\n" "{\"form\", \"from\"},\n" "{\"FPS\", \"First Person Shooter\"},\n" "{\"fragged\", \"killed\"},\n" "{\"ftw\", \"for the win\"},\n" "{\"G2G\", \"Got To Go\"},\n" "{\"GA\", \"Go Ahead\"},\n" "{\"GD\", \"God Damn\"},\n" "{\"GF\", \"Good Fight\"},\n" "{\"GG\", \"Good Game\"},\n" "{\"GGA\", \"Good Game All\"},\n" "{\"gimme\", \"Give me\"},\n" "{\"GL\", \"Good Luck\"},\n" "{\"Going Gold\", \"publishing of software\"},\n" "{\"grill\", \"girl\"},\n" "{\"guise\", \"guys\"},\n" "{\"h@\", \"Hat\"},\n" "{\"h4x\", \"hacks\"},\n" "{\"h4x0r\", \"hacker\"},\n" "{\"h8\", \"Hate\"},\n" "{\"hai\", \"hi\"},\n" "{\"high-ping bastard\", \"crappy internet\"},\n" "{\"HP\", \"Health Points\"},\n" "{\"IIRC\", \"If I Recall Correctly\"},\n" "{\"IMHO\", \"In My Honest Opinion\"},\n" "{\"IMO\", \"In My Opinion\"},\n" "{\"instagib\", \"unlimited ammo\"},\n" "{\"IRC\", \"Internet Relay Chat\"},\n" "{\"IRL\", \"In Real Life\"},\n" "{\"j0\", \"Yo!\"},\n" "{\"J00\", \"You!\"},\n" "{\"JJ\", \"Just Joking\"},\n" "{\"JK\", \"Just Kidding\"},\n" "{\"kewl\", \"cool\"},\n" "{\"kicked\", \"banned cheater\"},\n" "{\"kk\", \"ok\"},\n" "{\"KOS\", \"Kill On Sight\"},\n" "{\"l@ym\", \"laughing at your momma\"},\n" "{\"l337\", \"elite\"},\n" "{\"l33t\", \"Elite\"},\n" "{\"l4m3r\", \"lamer\"},\n" "{\"level up\", \"Improving\"},\n" "{\"liek\", \"like\"},\n" "{\"lobby\", \"chat room\"},\n" "{\"LOL\", \"Laughs out loud\"},\n" "{\"lopl\", \"lol\"},\n" "{\"low-ping bastard\", \"fast internet\"},\n" "{\"Lvl\", \"Level\"},\n" "{\"m2\", \"me too\"},\n" "{\"m8\", \"mate\"},\n" "{\"Mana\", \"cast magical spells\"},\n" "{\"mastah\", \"master\"},\n" "{\"max0r\", \"max\"},\n" "{\"Med\", \"Medic\"},\n" "{\"meh\", \"oh well\"},\n" "{\"MMORPG\", \"Massive Multiplayer Online Role Playing Game\"},\n" "{\"Mod\", \"A modification of a game\"},\n" "{\"MP\", \"Multiplayer\"},\n" "{\"MTFBWY\", \"May The Force Be With You\"},\n" "{\"n00b\", \"newbie\"},\n" "{\"N1\", \"Nice one\"},\n" "{\"Nade\", \"Grenade\"},\n" "{\"nd\", \"and\"},\n" "{\"Nerfed\", \"bad bug\"},\n" "{\"newb\", \"newcomer\"},\n" "{\"nooblet\", \"newcomer\"},\n" "{\"np\", \"No problem\"},\n" "{\"NT\", \"nice try\"},\n" "{\"OLO\", \"LOL\"},\n" "{\"OMDG\", \"oh my dance\"},\n" "{\"OMFG\", \"Oh My Fucking God\"},\n" "{\"OMG\", \"oh my god\"},\n" "{\"OMGWTFBBQ\", \"Oh My God What The Fuck Barbecue\"},\n" "{\"OMW\", \"On My Way\"},\n" "{\"OOR\", \"Out Of Range\"},\n" "{\"OOS\", \"Out Of Sight\"},\n" "{\"ORLY\", \"Oh, really?\"},\n" "{\"ORLY\", \"Oh, really?\"},\n" "{\"OSOK\", \"One Shot One Kill\"},\n" "{\"own\", \"own\"},\n" "{\"ownage\", \"superior\"},\n" "{\"p0wn\", \"pawned\"},\n" "{\"ph33r\", \"Fear me\"},\n" "{\"phear\", \"fear\"},\n" "{\"PITA\", \"Pain In The Arse/Ass\"},\n" "{\"PK\", \"Player Killing\"},\n" "{\"pl0x\", \"please\"},\n" "{\"PMGZ\", \"OMG\"},\n" "{\"PMSL\", \"Pissing MySelf Laughing\"},\n" "{\"Polishing\", \"final touches\"},\n" "{\"Port\", \"Teleport\"},\n" "{\"powwah\", \"power\"},\n" "{\"Pr0n\", \"porn\"},\n" "{\"pwnage\", \"superior\"},\n" "{\"pwnt\", \"superior\"},\n" "{\"r\", \"are\"},\n" "{\"r0x0r\", \"dude that rocks!\"},\n" "{\"rgr\", \"Roger\"},\n" "{\"ROFL\", \"Rolls on the floor laughing\"},\n" "{\"ROFLMAO\", \"Rolling On the Floor Laughing My Ass Off\"},\n" "{\"rox\", \"Rocks\"},\n" "{\"RP\", \"Role Play\"},\n" "{\"RPG\", \"Role-Playing Game\"},\n" "{\"RT\", \"Roger That\"},\n" "{\"RTFM\", \"Read the f***ing manual\"},\n" "{\"RTS\", \"Real-Time Strategy\"},\n" "{\"Sig\", \"Signature\"},\n" "{\"sk1llz\", \"Skills\"},\n" "{\"Slug\", \"A large, non-self-propelled projectile\"},\n" "{\"smat\", \"smart\"},\n" "{\"SP\", \"Single Player\"},\n" "{\"Spam\", \"Repeatedly messaging the same messages\"},\n" "{\"spawn\", \"reborn\"},\n" "{\"sry\", \"Sorry\"},\n" "{\"STFU\", \"Shut The Fuck UP\"},\n" "{\"sup\", \"What's Up\"},\n" "{\"sux0r\", \"Sucks\"},\n" "{\"taunt\", \"Provoking\"},\n" "{\"TBH\", \"To Be Honest\"},\n" "{\"teh\", \"the\"},\n" "{\"Thx\", \"Thanks\"},\n" "{\"TPS\", \"Third Person Shooter\"},\n" "{\"Ty\", \"Thank you\"},\n" "{\"u\", \"you\"},\n" "{\"w8\", \"wait\"},\n" "{\"wat\", \"what\"},\n" "{\"WB\", \"Welcome Back\"},\n" "{\"wen\", \"when\"},\n" "{\"whut\", \"what\"},\n" "{\"winnar\", \"winner\"},\n" "{\"wot\", \"what\"},\n" "{\"WTF\", \"What the f***?\"},\n" "{\"WTG\", \"Way To Go\"},\n" "{\"WYG\", \"What You Got?\"},\n" "{\"y\", \"why\"},\n" "{\"ya\", \"yeah\"},\n" "{\"yass\", \"yes\"},\n" "{\"yea\", \"yeah\"},\n" "{\"yos\", \"yes\"},\n" "{\"yus\", \"yes\"},\n" "{\"ZPMG\", \"OMG\"},") 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