Regex Cheat Sheet for Sysadmins and Developers
Core syntax, real-world patterns for IT work, and the mistakes โ greedy matching, unescaped dots, catastrophic backtracking โ that trip people up most.
Why Regex Is Worth Knowing
Regular expressions let you search, validate and extract text by pattern instead of exact match โ parsing log files, validating form input, filtering firewall logs by IP, or bulk-renaming files. A handful of building blocks cover the vast majority of real-world use, and this page is a working reference for exactly those.
Core Syntax Reference
^โ start of string/line$โ end of string/line.โ any single character (except newline)\dโ any digit (0-9);\Dโ any non-digit\wโ any word character (letters, digits, underscore);\Wโ the opposite\sโ any whitespace character;\Sโ the opposite*โ zero or more of the previous token;+โ one or more;?โ zero or one{n,m}โ between n and m repetitions;{n}โ exactly n[abc]โ any one of a, b or c;[^abc]โ any character except a, b or c;[a-z]โ a range()โ a capturing group;(?:)โ a non-capturing group|โ alternation (this OR that)(?=...)โ positive lookahead;(?!...)โ negative lookahead
Practical Patterns for IT Work
Matching an IPv4 address (simple version, doesn't reject invalid octets over 255):
Extracting a timestamp from a log line like 2026-09-15 14:32:07 ERROR disk full:
A reasonable email pattern for basic validation (not a full RFC 5322 implementation, but fine for most sanity checks):
Matching a Windows file path:
Test and refine any of these live with the Regex Tester โ it highlights matches and captured groups as you type, including a built-in library of common patterns.
Common Mistakes
- Forgetting to escape special characters. A literal dot in an IP address needs to be written
\.โ an unescaped.matches any character, which is why192.168.1.1would also incorrectly match something like192a168a1a1. - Greedy vs. lazy matching. By default,
*and+are greedy โ they match as much as possible. Adding?after them (e.g..*?) makes them lazy, matching as little as possible. This matters a lot when extracting content between two delimiters in a line that contains the delimiter more than once. - Catastrophic backtracking. Patterns with nested repetition (like
(a+)+) can cause the regex engine to try an exponential number of combinations on certain inputs, effectively hanging. Keep nested quantifiers simple and specific where possible. - Not anchoring the pattern. Without
^and$, a pattern can match a substring anywhere in the text rather than the whole string, which is rarely what you want for validation.
Tools For This Guide
Frequently Asked Questions
Test Your Regex Live
Live match highlighting, group capture, and a built-in library of common patterns to start from.