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

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 AutoIt, please visit: https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm