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
No Match

r"
"
gms

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"<a[\S ]*href=\"([a-zA-Z0-9\/\.:]*)\"[\S ]*[\s]*><img[\S ]*src=\"([a-zA-Z0-9\/\.]*)\"[\S ]*[\s]*\/{0,1}><\/a>" test_str = ("<!DOCTYPE html>\n" "<html lang=\"en\">\n" " <head>\n" " <meta charset=\"utf-8\" />\n" " <title>maia :3</title>\n" " \n" " <link rel=\"stylesheet\" href=\"/style.css\"/>\n" " <link rel=\"stylesheet\" href=\"/prism.css\"/>\n\n" " <meta name=\"description\" content=\"meow\" />\n" " <meta name='keywords' content='nyancrimew,maia arson crimew,android,switzerland,hacktivism,lucerne,developer,hacktivism'>\n" " <meta property=\"og:title\" content=\"maia :3\">\n" " <meta property=\"og:description\" content=\"meow\">\n" " <meta property=\"og:url\" content=\"https://maia.crimew.gay/\">\n" " <meta property=\"og:image\" content=\"https://maia.crimew.gay/img/cover.jpg\">\n" " \n" " <meta property=\"og:site_name\" content=\"maia :3\">\n" " <meta name=\"article:author\" content=\"maia arson crimew\">\n" " <meta name=\"article:published_time\" content=\"2023-01-21T17:21:09.000Z\">\n" " <meta name=\"twitter:card\" content=\"summary_large_image\">\n" " <meta name=\"twitter:site\" content=\"@_nyancrimew\">\n" " <meta name=\"twitter:creator\" content=\"@_nyancrimew\">\n" " <meta name=\"theme-color\" content=\"#eca9cb\">\n" " <link rel=\"apple-touch-icon\" sizes=\"180x180\" href=\"/img/apple-touch-icon.png\">\n" " <link rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/img/favicon-32x32.png\">\n" " <link rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/img/favicon-16x16.png\">\n" " <link rel=\"shortcut icon\" href=\"/favicon.ico\">\n\n" " <link rel=\"alternate\" type=\"application/rss+xml\" href=\"/feed.xml\" />\n" " </head>\n" " <body>\n" " <header id=\"site_header\">\n" " <h1>maia <sub>arson crimew</sub></h1>\n" " <nav>\n" " \n" " <span><a href=\"/\" class=\"active\">home</a></span>\n" " \n" " <span>|</span>\n" " \n" " \n" " <span><a href=\"/posts/\" >blog</a></span>\n" " \n" " <span>|</span>\n" " \n" " \n" " <span><a href=\"/samples/\" >sample packs</a></span>\n" " \n" " \n" "</nav>\n" " </header>\n" " <div id=\"content\">\n" " \n" "<div style=\"display: flex; flex-wrap: wrap; justify-content: center;\">\n" " <div style=\"flex-grow: 1; flex-shrink: 1; flex-basis: 30%; min-width: 200px; max-width: 70%;\">\n" " <img src=\"/img/kitten_cropped.png\" alt=\"a drawing of a small white kitten with black spots sitting and looking at you\">\n" " </div>\n" " <div style=\"flex-grow: 2; flex-shrink: 2; flex-basis: 60%;\">\n" " <p>\n" " hello i am maia arson crimew <sup>(it/she)</sup> and i am gay, mostly for girls, and i'm a tiny kitten :3\n" " </p>\n" " <p>\n" " i know lot's of things about cyber security, programming and far too little about music but i still\n" " try to do all those things at once. if you like the things i do here or on social media you can\n" " <a href=\"https://ko-fi.com/nyancrimew\" target=\"_blank\">support me on ko-fi</a>. links to the various things i do\n" " can be found in the footer. if you're looking for some more serious info about me, \n" " <a href=\"https://en.wikipedia.org/wiki/maia_arson_crimew\" target=\"_blank\">there is wikipedia for that</a>.\n" " </p>\n" " </div>\n" "</div>\n\n" " </div>\n" " <footer>\n" " <div class=\"footer-kitten\"><img src=\"/img/walkingkitten_crop.png\" alt=\"a small white kitten with black spots walking across the screen\"></div>\n" " <div>\n" " \n" " <span><a href=\"https://maia.crimew.gay/feed.xml\" rel=\"me\" target=\"_blank\">rss feed</a></span>\n" " \n" " <span>|</span>\n" " \n" " \n" " <span><a href=\"https://twitter.com/_nyancrimew\" rel=\"me\" target=\"_blank\">birded site</a></span>\n" " \n" " <span>|</span>\n" " \n" " \n" " <span><a href=\"https://crimew.gay/maia\" rel=\"me\" target=\"_blank\">fedded verse</a></span>\n" " \n" " <span>|</span>\n" " \n" " \n" " <span><a href=\"https://soundcloud.com/nyancrimew\" rel=\"me\" target=\"_blank\">sounded cloud</a></span>\n" " \n" " <span>|</span>\n" " \n" " \n" " <span><a href=\"https://last.fm/user/nyancrimew\" rel=\"me\" target=\"_blank\">last dot federated states of micronesia</a></span>\n" " \n" " <span>|</span>\n" " \n" " \n" " <span><a href=\"https://github.com/nyancrimew\" rel=\"me\" target=\"_blank\">gitted hub</a></span>\n" " \n" " <span>|</span>\n" " \n" " \n" " <span><a href=\"https://git.lavender.software/nyancrimew\" rel=\"me\" target=\"_blank\">gitted tea</a></span>\n" " \n" " \n" " </div>\n" " <div class=\"badges\">\n" " \n" " <a href=\"https://maia.crimew.gay\" target=\"_blank\"><img src=\"/badges/maia.crimew.gay.png\" alt=\"maia crimew\" title=\"my site :3\"></a>\n" " \n" " <a href=\"https://cyber.dabamos.de/88x31\" target=\"_blank\"><img src=\"/badges/88x31.gif\" alt=\"88x31\" title=\"baaaadges\"></a>\n" " \n" " <a href=\"javascript:alert(&#39;what did i tell you??&#39;)\" target=\"_blank\"><img src=\"/badges/noclick.gif\" alt=\"don&#39;t click here\" title=\"don&#39;t do it\"></a>\n" " \n" " <img src=\"/badges/acab.gif\" alt=\"acab\" title=\"fuck the cops\">\n" " \n" " <a href=\"https://archlinux.org\" target=\"_blank\"><img src=\"/badges/archlinux.gif\" alt=\"arch linux\" title=\"i use arch btw\"></a>\n" " \n" " <a href=\"https://versary.town\" target=\"_blank\"><img src=\"/badges/versarytown.png\" alt=\"versary town\" title=\"a very cool website by a very cool person &lt;3\"></a>\n" " \n" " <a href=\"https://goop.house\" target=\"_blank\"><img src=\"/badges/goop.gif\" alt=\"goop house\" title=\"goop house next up\"></a>\n" " \n" " <img src=\"/badges/anarchynow.gif\" alt=\"anarchy now\" >\n" " \n" " <a href=\"https://sleepy.zone\" target=\"_blank\"><img src=\"/badges/sleepy.png\" alt=\"sleepy zone\" title=\"sleepy.zone is the best internet radio out there\"></a>\n" " \n" " <img src=\"/badges/kitten88.gif\" alt=\"kitten&#39;s corner\" title=\"im a kitten fr fr\">\n" " \n" " <a href=\"https://oat.zone\" target=\"_blank\"><img src=\"/badges/oatzone.gif\" alt=\"oat.zone\" ></a>\n" " \n" " <img src=\"/badges/slimesnow.png\" alt=\"slimes now\" >\n" " \n" " <img src=\"/badges/slugcat.png\" alt=\"slugcat\" >\n" " \n" " <a href=\"https://sinewave.cyou\" target=\"_blank\"><img src=\"/badges/sinewave.gif\" alt=\"sinewave\" ></a>\n" " \n" " <img src=\"/badges/nonbinary.png\" alt=\"non-binary pride\" title=\"im non binary fr fr\">\n" " \n" " <img src=\"/badges/queer.png\" alt=\"queer pride\" title=\"im queeeer as fuck\">\n" " \n" " <img src=\"/badges/piracy.gif\" alt=\"piracy now\" title=\"piracy is swag and you should do it\">\n" " \n" " <a href=\"https://utsuho.rocks\" target=\"_blank\"><img src=\"/badges/utsuhorocks.png\" alt=\"utsuho rocks\" title=\"utsuho rocks\"></a>\n" " \n" " <a href=\"https://ilwag.com\" target=\"_blank\"><img src=\"/badges/ilwagbannersmol.png\" alt=\"ilwag.com\" title=\"cute lil tunes :3\"></a>\n" " \n" " </div>\n" " <script src=\"https://lavender.software/webring/webring-0.2.0.js\" data-site-id=\"maia\"></script>\n" "</footer>\n" " </body>\n" "</html>\n") matches = re.finditer(regex, test_str, re.MULTILINE | re.DOTALL) 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