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"maxiobe request response game" test_str = ("index.php:\n\n" "<?php\n" "// index.php – Aufgabe 3 (Bootstrap, Bereichsauswahl, Validierung, Versuchszähler)\n" "declare(strict_types=1);\n" "session_start();\n\n" "/* ------------------ Helper ------------------ */\n" "function h(string $s): string { return htmlspecialchars($s, ENT_QUOTES, 'UTF-8'); }\n" "function resetGame(): void {\n" " unset($_SESSION['randomNumber'], $_SESSION['tries'], $_SESSION['min'], $_SESSION['max'], $_SESSION['guesses']);\n" "}\n\n" "/* ------------------ Init ------------------ */\n" "$alerts = []; // sammelt Bootstrap-Alerts [ ['type'=>'warning|danger|success|info','text'=>'...'] ]\n" "if (!isset($_SESSION['tries'])) { $_SESSION['tries'] = 0; }\n" "if (!isset($_SESSION['guesses'])) { $_SESSION['guesses'] = []; }\n\n" "/* ------------------ Actions ------------------ */\n" "if ($_SERVER['REQUEST_METHOD'] === 'POST') {\n\n" " // Exit / Neues Spiel\n" " if (isset($_POST['exit'])) {\n" " resetGame();\n" " header('Location: ' . $_SERVER['SCRIPT_NAME']);\n" " exit();\n" " }\n\n" " // Start-Spiel mit Bereich (min/max)\n" " if (isset($_POST['startgame'])) {\n" " $min = $_POST['min'] ?? '1';\n" " $max = $_POST['max'] ?? '10';\n\n" " // Grundvalidierung: ganzzahlig\n" " $validInts = ctype_digit(strval($min)) && ctype_digit(strval($max));\n" " if (!$validInts) {\n" " $alerts[] = ['type'=>'danger','text'=>'Bitte nur ganze Zahlen für <b>Min</b> und <b>Max</b> verwenden.'];\n" " } else {\n" " $min = (int)$min;\n" " $max = (int)$max;\n\n" " // Logische Prüfungen\n" " if ($min < 0) { $alerts[] = ['type'=>'warning','text'=>'Der Minimalwert darf nicht negativ sein.']; }\n" " if ($max <= $min) { $alerts[] = ['type'=>'warning','text'=>'<b>Max</b> muss größer als <b>Min</b> sein.']; }\n" " if ($max - $min > 10000) { $alerts[] = ['type'=>'warning','text'=>'Der Bereich ist zu groß. Wähle einen kleineren Abstand (max. 10.000).']; }\n\n" " if (!$alerts) {\n" " $_SESSION['min'] = $min;\n" " $_SESSION['max'] = $max;\n" " $_SESSION['randomNumber'] = rand($min, $max);\n" " $_SESSION['tries'] = 0;\n" " $_SESSION['guesses'] = [];\n" " $alerts[] = ['type'=>'primary','text'=>\"Spiel gestartet! Rate die Zahl zwischen <b>{$min}</b> und <b>{$max}</b>.\"];\n" " }\n" " }\n" " }\n\n" " // Rateversuch\n" " if (isset($_POST['try']) && isset($_SESSION['randomNumber'])) {\n" " $raw = $_POST['enteredNumber'] ?? '';\n\n" " // Prüfung: Zahl eingegeben?\n" " if ($raw === '' || !preg_match('/^-?\\d+$/', trim((string)$raw))) {\n" " $alerts[] = ['type'=>'warning','text'=>'Bitte eine <b>ganze Zahl</b> eingeben.'];\n" " } else {\n" " $guess = (int)$raw;\n" " $min = $_SESSION['min'] ?? 1;\n" " $max = $_SESSION['max'] ?? 10;\n\n" " // Bereichsprüfung\n" " if ($guess < $min || $guess > $max) {\n" " $alerts[] = ['type'=>'warning','text'=>\"Dein Tipp liegt <b>außerhalb</b> des Bereichs <b>{$min}–{$max}</b>.\"];\n" " } else {\n" " $secret = (int)$_SESSION['randomNumber'];\n" " $_SESSION['tries']++;\n" " $_SESSION['guesses'][] = $guess;\n\n" " if ($guess === $secret) {\n" " $t = (int)$_SESSION['tries'];\n" " $alerts[] = ['type'=>'success','text'=>\"Zahl erraten 🎉 – du brauchtest <b>{$t}</b> Versuch\" . ($t === 1 ? '' : 'e') . \".\"];\n" " // Nach Erfolg: Neues Spiel möglich (Reset-Button rendern), Zahl bleibt aber bis Exit,\n" " // damit man die Success-Meldung sieht und \"Neues Spiel\" klicken kann.\n" " // Alternativ direkt resetGame(); echo Link – aber wir bleiben konsistent mit Aufgabe 2.\n" " } else {\n" " $hint = ($guess > $secret) ? 'zu hoch' : 'zu niedrig';\n" " $alerts[] = ['type'=>'danger','text'=>\"Leider falsch – dein Tipp ist <b>{$hint}</b>.\"];\n" " }\n" " }\n" " }\n" " }\n" "}\n\n" "/* ------------------ View (HTML) ------------------ */\n" "?>\n" "<!doctype html>\n" "<html lang=\"de\">\n" "<head>\n" " <meta charset=\"utf-8\"/>\n" " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>\n" " <title>Ratespiel – Aufgabe 3</title>\n" " <link rel=\"stylesheet\" href=\"assets/bootstrap/css/bootstrap.min.css\">\n" "</head>\n" "<body>\n" "<div class=\"container py-4\">\n" " <h1 class=\"mb-4\">Ratespiel – Aufgabe 3</h1>\n\n" " <?php foreach ($alerts as $a): ?>\n" " <div class=\"alert alert-<?php echo h($a['type']); ?>\" role=\"alert\">\n" " <?php echo $a['text']; ?>\n" " </div>\n" " <?php endforeach; ?>\n\n" " <?php if (isset($_SESSION['randomNumber'])): ?>\n" " <!-- Laufendes Spiel -->\n" " <div class=\"card mb-3\">\n" " <div class=\"card-body\">\n" " <h5 class=\"card-title\">Dein Tipp</h5>\n" " <form class=\"row g-3\" action=\"<?php echo h($_SERVER['SCRIPT_NAME']); ?>\" method=\"post\" name=\"form\">\n" " <div class=\"col-sm-6 col-md-4\">\n" " <input class=\"form-control\" type=\"number\" name=\"enteredNumber\"\n" " placeholder=\"Zahl <?php echo (int)($_SESSION['min'] ?? 1); ?>–<?php echo (int)($_SESSION['max'] ?? 10); ?>\"\n" " min=\"<?php echo (int)($_SESSION['min'] ?? 1); ?>\"\n" " max=\"<?php echo (int)($_SESSION['max'] ?? 10); ?>\">\n" " </div>\n" " <div class=\"col-auto\">\n" " <button class=\"btn btn-success\" type=\"submit\" name=\"try\" value=\"1\">Raten</button>\n" " </div>\n" " <div class=\"col-auto\">\n" " <button class=\"btn btn-outline-secondary\" type=\"submit\" name=\"exit\" value=\"1\">Neues Spiel</button>\n" " </div>\n" " </form>\n" " <p class=\"mt-3 mb-0\">\n" " Bereich: <span class=\"badge bg-secondary\">\n" " <?php echo (int)($_SESSION['min'] ?? 1); ?>–<?php echo (int)($_SESSION['max'] ?? 10); ?>\n" " </span>\n" " &nbsp;|&nbsp;\n" " Versuche: <span class=\"badge bg-info text-dark\"><?php echo (int)$_SESSION['tries']; ?></span>\n" " </p>\n" " <?php if (!empty($_SESSION['guesses'])): ?>\n" " <p class=\"mt-2 mb-0\">\n" " Bisherige Tipps:\n" " <?php foreach ($_SESSION['guesses'] as $g): ?>\n" " <span class=\"badge bg-light text-dark\"><?php echo (int)$g; ?></span>\n" " <?php endforeach; ?>\n" " </p>\n" " <?php endif; ?>\n" " </div>\n" " </div>\n\n" " <?php if (!empty($alerts) && end($alerts)['type'] === 'success'): ?>\n" " <!-- Bei Erfolg optional Hinweis-Card -->\n" " <div class=\"alert alert-success\" role=\"alert\">\n" " Glückwunsch! Du kannst oben auf <b>Neues Spiel</b> klicken, um erneut zu starten.\n" " </div>\n" " <?php endif; ?>\n\n" " <?php else: ?>\n" " <!-- Startseite mit Bereichsauswahl -->\n" " <div class=\"card\">\n" " <div class=\"card-body\">\n" " <h5 class=\"card-title\">Spiel starten</h5>\n" " <form class=\"row g-3\" action=\"<?php echo h($_SERVER['SCRIPT_NAME']); ?>\" method=\"post\" name=\"form\">\n" " <div class=\"col-sm-4 col-md-3\">\n" " <label class=\"form-label\" for=\"min\">Min</label>\n" " <input class=\"form-control\" type=\"number\" id=\"min\" name=\"min\" value=\"1\" min=\"0\" step=\"1\" required>\n" " </div>\n" " <div class=\"col-sm-4 col-md-3\">\n" " <label class=\"form-label\" for=\"max\">Max</label>\n" " <input class=\"form-control\" type=\"number\" id=\"max\" name=\"max\" value=\"10\" min=\"1\" step=\"1\" required>\n" " </div>\n" " <div class=\"col-12\">\n" " <button class=\"btn btn-primary\" type=\"submit\" name=\"startgame\" value=\"1\">Spielstart</button>\n" " </div>\n" " </form>\n" " <p class=\"text-muted mt-2 mb-0\">Tipp: Wähle z. B. 1–100 für mehr Herausforderung.</p>\n" " </div>\n" " </div>\n" " <?php endif; ?>\n\n" "</div>\n" "<script src=\"assets/bootstrap/js/bootstrap.bundle.min.js\"></script>\n" "</body>\n" "</html>\n\n" "form try:\n\n" "<?php\n" "// form_try.part.php\n" "?>\n" "<form action=\"<?php echo htmlspecialchars($_SERVER['SCRIPT_NAME']); ?>\" method=\"post\" name=\"form\">\n" " <input type=\"text\" name=\"enteredNumber\" placeholder=\"Zahl zwischen 1 und 10\" />\n" " <input type=\"submit\" value=\"Raten\" name=\"try\" />\n" " <input type=\"submit\" value=\"Exit\" name=\"exit\" />\n" "</form>\n\n\n" "form start:\n\n" "<?php\n" "// form_start.part.php\n" "?>\n" "<form action=\"<?php echo htmlspecialchars($_SERVER['SCRIPT_NAME']); ?>\" method=\"post\" name=\"form\">\n" " <input type=\"submit\" value=\"Spielstart\" name=\"startgame\" />\n" "</form>\n\n\n") 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