Regulare Ausdrucke (Regex) gehoren zu den machtigsten Werkzeugen eines Entwicklers. Ob Sie Benutzereingaben validieren, Logdateien parsen oder komplexe Suchen-und-Ersetzen-Operationen durchfuhren -- Regex bietet unerreichte Prazision. Dieses vollstandige Regex-Cheat-Sheet deckt alle wichtigen Konzepte ab.
Testen Sie jedes Muster live in unserem Regex Tester ->
Grundsyntax: Anker und Zeichenklassen
Anker und Zeichenklassen bilden die Grundlage. Anker geben wo in der Zeichenkette an. Zeichenklassen definieren welche Zeichen passen.
Anker
Anker entsprechen keinen Zeichen, sondern Positionen in der Zeichenkette.
| Pattern | Description | Example |
|---|---|---|
^ | Start of string (or line with m flag) | ^Hello matches "Hello World" |
$ | End of string (or line with m flag) | world$ matches "Hello world" |
\b | Word boundary | \bcat\b matches "cat" but not "catch" |
\B | Non-word boundary | \Bcat\B matches "concatenate" |
\A | Absolute start of string (Python, Ruby) | \AHello |
\Z | Absolute end of string (Python, Ruby) | bye\Z |
Zeichenklassen
Zeichenklassen matchen eine Menge von Zeichen an einer Position.
| Pattern | Description | Equivalent |
|---|---|---|
[abc] | Match a, b, or c | -- |
[^abc] | Match anything except a, b, c | -- |
[a-z] | Match any lowercase letter | -- |
[A-Z] | Match any uppercase letter | -- |
[0-9] | Match any digit | \d |
. | Match any character (except newline by default) | -- |
\d | Digit | [0-9] |
\D | Non-digit | [^0-9] |
\w | Word character | [a-zA-Z0-9_] |
\W | Non-word character | [^a-zA-Z0-9_] |
\s | Whitespace (space, tab, newline) | [ \t\n\r\f\v] |
\S | Non-whitespace | [^ \t\n\r\f\v] |
Sonderzeichen (Metazeichen)
Diese Zeichen haben spezielle Bedeutung. Zum wortlichen Matchen mit Backslash escapen.
Special characters that need escaping:
. ^ $ * + ? { } [ ] \ | ( )
To match a literal dot: \.
To match a literal star: \*
To match a literal pipe: \|
To match a backslash: \\Quantifikatoren: Wie oft matchen
Quantifikatoren steuern die Wiederholungen. Standard ist gierig. Mit ? werden sie faul.
| Greedy | Lazy | Description |
|---|---|---|
* | *? | 0 or more times |
+ | +? | 1 or more times |
? | ?? | 0 or 1 time (optional) |
{n} | {n}? | Exactly n times |
{n,} | {n,}? | n or more times |
{n,m} | {n,m}? | Between n and m times |
Gierig vs. Faul
Das gierige Muster <.*> matcht die gesamte Zeichenkette, wahrend <.*?> nur das erste Tag matcht.
// Input string:
const str = '<b>bold</b> and <i>italic</i>';
// Greedy: matches from first < to LAST >
str.match(/<.*>/);
// Result: '<b>bold</b> and <i>italic</i>'
// Lazy: matches from first < to FIRST >
str.match(/<.*?>/);
// Result: '<b>'Gruppen und Erfassung
Gruppen behandeln mehrere Zeichen als Einheit und ermoglichen die Extraktion.
Erfassungsgruppen
Klammern (...) erfassen den Match. Ruckreferenzen: \1, \2.
// Capturing group example
const dateRegex = /^(\d{4})-(\d{2})-(\d{2})$/;
const match = '2026-02-10'.match(dateRegex);
// match[0] = '2026-02-10' (full match)
// match[1] = '2026' (year)
// match[2] = '02' (month)
// match[3] = '10' (day)
// Backreference: match repeated words
const repeated = /\b(\w+)\s+\1\b/;
repeated.test('the the'); // true
repeated.test('the cat'); // falseBenannte Gruppen
Benannte Gruppen mit (?<name>...) verbessern die Lesbarkeit.
// Named groups in JavaScript
const dateRegex = /^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})$/;
const match = '2026-02-10'.match(dateRegex);
// match.groups.year = '2026'
// match.groups.month = '02'
// match.groups.day = '10'# Named groups in Python
import re
pattern = r'^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})$'
m = re.match(pattern, '2026-02-10')
# m.group('year') = '2026'
# m.group('month') = '02'
# m.group('day') = '10'Nicht-erfassende Gruppen
(?:...) fur Gruppierung ohne Erfassung.
// Non-capturing group
const regex = /(?:https?|ftp):\/\/[^\s]+/;
// Groups the protocol options without capturing them
// Only the full URL is in match[0]Alternation (ODER)
Der Pipe | fur Alternativen.
// Alternation examples
/cat|dog/ // Match "cat" or "dog"
/(red|blue) car/ // Match "red car" or "blue car"
/^(GET|POST|PUT|DELETE)\s/ // Match HTTP methodsLookahead und Lookbehind
Lookarounds prufen, ob ein Muster vor oder nach der Position existiert, ohne Zeichen zu verbrauchen.
| Syntax | Type | Description |
|---|---|---|
(?=...) | Positive lookahead | What follows must match |
(?!...) | Negative lookahead | What follows must NOT match |
(?<=...) | Positive lookbehind | What precedes must match |
(?<!...) | Negative lookbehind | What precedes must NOT match |
Positiver Lookahead (?=...)
Position gefolgt von dem Muster.
// Match a number followed by "px"
/\d+(?=px)/
// "20px 30em 40px" → matches "20" and "40" (not "30")
// Password: must contain at least one digit
/^(?=.*\d).{8,}$/Negativer Lookahead (?!...)
Position nicht gefolgt von dem Muster.
// Match "cat" NOT followed by "fish"
/cat(?!fish)/
// "catfish catdog" → matches "cat" in "catdog" only
// Match numbers NOT followed by a unit
/\d+(?!\s*(px|em|rem|%))/Positiver Lookbehind (?<=...)
Position vorausgegangen von dem Muster.
// Match a number preceded by "$"
/(?<=\$)\d+(\.\d{2})?/
// "$49.99 and €29.99" → matches "49.99" only
// Extract value after "price:"
/(?<=price:\s*)\d+/Negativer Lookbehind (?<!...)
Position nicht vorausgegangen von dem Muster.
// Match "cat" NOT preceded by "wild"
/(?<!wild)cat/
// "wildcat housecat" → matches "cat" in "housecat" only
// Match digits not preceded by a minus sign
/(?<!-)\b\d+\b/Hinweis: Lookbehinds werden nicht von allen Engines unterstutzt.
Regex-Flags (Modifikatoren)
Flags andern die Interpretation des Musters.
| Flag | Name | Description | Example |
|---|---|---|---|
g | Global | Find all matches, not just the first | /cat/g finds all "cat" occurrences |
i | Case-insensitive | Match upper and lowercase interchangeably | /hello/i matches "Hello", "HELLO" |
m | Multiline | ^ and $ match line starts/ends | /^start/m matches at each line start |
s | Dotall (Single-line) | . matches newline characters too | /a.b/s matches "a\nb" |
u | Unicode | Enable full Unicode matching | /\u{1F600}/u matches emoji |
y | Sticky | Match only at lastIndex position | Used for tokenizing / lexing |
// Combining flags
const regex = /^hello world$/gim;
// In Python, flags are constants:
import re
pattern = re.compile(r'^hello world$', re.IGNORECASE | re.MULTILINE)
// In Go, use inline flags:
// (?i) for case-insensitive, (?m) for multiline, (?s) for dotall
regexp.MustCompile("(?im)^hello world$")Haufige Muster - Kurzreferenz
Die am haufigsten benotigten Regex-Muster auf einen Blick.
| Use Case | Pattern |
|---|---|
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ | |
| URL (HTTP/S) | ^https?:\/\/[^\s]+$ |
| IPv4 Address | ^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$ |
| Date (YYYY-MM-DD) | ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ |
| Time (HH:MM:SS) | ^([01]\d|2[0-3]):[0-5]\d:[0-5]\d$ |
| Hex Color | ^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$ |
| Strong Password | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&]).{8,}$ |
| Phone (E.164) | ^\+[1-9]\d{1,14}$ |
| UUID | ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$ |
| Semantic Version | ^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[\w.]+)?(\+[\w.]+)?$ |
| HTML Tag | <([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>(.*?)<\/\1> |
| Trim Whitespace | ^\s+|\s+$ |
Alle Muster mit unserem Regex Tester testen ->
Regex in verschiedenen Programmiersprachen
Die Syntax ist grosstenteils universell, aber jede Sprache hat ihre eigene API.
JavaScript
// Creating regex in JavaScript
const regex1 = /^\d+$/; // Literal syntax
const regex2 = new RegExp('^\\d+$'); // Constructor (needs double-escape)
// Testing
regex1.test('12345'); // true
// Matching
'hello world'.match(/\w+/g); // ['hello', 'world']
// Replacing
'2026-02-10'.replace(
/^(\d{4})-(\d{2})-(\d{2})$/,
'$2/$3/$1'
); // '02/10/2026'
// matchAll (ES2020) - get all matches with groups
const text = 'Price: $10, Tax: $2';
for (const m of text.matchAll(/\$(\d+)/g)) {
console.log(m[0], m[1]);
// '$10' '10', then '$2' '2'
}
// Named groups (ES2018+)
const { groups } = '2026-02-10'.match(
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
);
console.log(groups.year); // '2026'Python
import re
# Compile for reuse (recommended)
pattern = re.compile(r'^\d+$')
# Test if the entire string matches
pattern.match('12345') # Match object (truthy)
pattern.match('abc') # None (falsy)
# Search anywhere in the string
re.search(r'\d+', 'abc 123 def') # Finds '123'
# Find all matches
re.findall(r'\w+', 'hello world') # ['hello', 'world']
# Replace
re.sub(
r'^(\d{4})-(\d{2})-(\d{2})$',
r'\2/\3/\1',
'2026-02-10'
) # '02/10/2026'
# Named groups
m = re.match(
r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})',
'2026-02-10'
)
m.group('year') # '2026'
m.group('month') # '02'
# Flags
re.findall(r'^start', text, re.MULTILINE | re.IGNORECASE)
# Split by pattern
re.split(r'[,;\s]+', 'a, b; c d') # ['a', 'b', 'c', 'd']Go (Golang)
package main
import (
"fmt"
"regexp"
)
func main() {
// Compile (panics on invalid pattern)
re := regexp.MustCompile(`^\d+$`)
// Test
fmt.Println(re.MatchString("12345")) // true
fmt.Println(re.MatchString("abc")) // false
// Find first match
re2 := regexp.MustCompile(`\d+`)
fmt.Println(re2.FindString("abc 123 def")) // "123"
// Find all matches
fmt.Println(re2.FindAllString("10 cats and 20 dogs", -1))
// ["10", "20"]
// Replace
re3 := regexp.MustCompile(`(\d{4})-(\d{2})-(\d{2})`)
result := re3.ReplaceAllString("2026-02-10", "$2/$3/$1")
fmt.Println(result) // "02/10/2026"
// Named groups
re4 := regexp.MustCompile(`(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})`)
match := re4.FindStringSubmatch("2026-02-10")
for i, name := range re4.SubexpNames() {
if name != "" {
fmt.Printf("%s: %s\n", name, match[i])
}
}
// year: 2026, month: 02, day: 10
// Inline flags: (?i) case-insensitive, (?m) multiline, (?s) dotall
re5 := regexp.MustCompile(`(?i)hello`)
fmt.Println(re5.MatchString("HELLO")) // true
}Go verwendet RE2, das Lookaheads, Lookbehinds und Ruckreferenzen nicht unterstutzt.
Performance-Tipps und Best Practices
Seien Sie spezifisch: [a-zA-Z] statt . verwenden.
Katastrophales Backtracking vermeiden: Verschachtelte Quantifikatoren wie (a+)+ vermeiden.
// BAD: Catastrophic backtracking risk
const bad = /^(a+)+$/;
bad.test('aaaaaaaaaaaaaaaaaaaaa!'); // Extremely slow!
// GOOD: Flatten nested quantifiers
const good = /^a+$/;
good.test('aaaaaaaaaaaaaaaaaaaaa!'); // Instant: falseNicht-erfassende Gruppen nutzen: (?:...) wenn keine Erfassung noetig ist.
Einmal kompilieren: In Python und Go die Regex einmal kompilieren und wiederverwenden.
# Python: compile once, reuse many times
import re
email_re = re.compile(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$')
# Fast: uses pre-compiled pattern
for addr in addresses:
if email_re.match(addr):
print(f"Valid: {addr}")Inkrementell testen: Komplexe Muster schrittweise aufbauen.
Testen Sie jedes Muster live in unserem Regex Tester ->
Haufig gestellte Fragen
Was ist ein Regex-Cheat-Sheet und warum brauche ich eins?
Ein Regex-Cheat-Sheet ist eine Kurzreferenz mit Syntax, Metazeichen, Quantifikatoren, Flags und haufigen Mustern. Auch erfahrene Entwickler konnen nicht alles auswendig.
Was ist der Unterschied zwischen .* und .*? in Regex?
.* ist gierig (langster Match), .*? ist faul (kurzester Match). Verwenden Sie faul, um beim ersten Begrenzer zu stoppen.
Unterstutzen alle Programmiersprachen dieselbe Regex-Syntax?
Die meisten unterstutzen PCRE. Erweiterte Features unterscheiden sich: JavaScript hat Lookbehinds seit ES2018, Go (RE2) unterstutzt sie nicht.
Wie kann ich Regex-Muster testen und debuggen?
Verwenden Sie einen interaktiven Regex-Tester wie unser Tool, das Matches in Echtzeit anzeigt.
Was ist katastrophales Backtracking?
Es tritt auf, wenn der Engine exponentiell lange braucht. Vermeiden Sie verschachtelte Quantifikatoren und seien Sie spezifisch.