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

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

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 Rust, please visit: https://docs.rs/regex/latest/regex/