Language cheatsheet
Regular expressions pcre2 Cheatsheet
Regex patterns for matching, grouping, lookarounds and common formats, plus how to use them in grep, sed, JavaScript and Python.
Characters and anchors
- Match digits — Match one or more digits. Use \D for anything that is not a digit.
- Match word characters — Match letters, digits and underscore. Use \W for the opposite.
- Match whitespace — Match spaces, tabs and line breaks. Use \S for the opposite.
- Match any character — Match any single character except a line break, unless the dotall flag is set.
- Match one of several characters — Match a single letter, either case.
- Match anything except some characters — Match one character that is not a digit.
- Match the whole line — Anchor to the start and end of the text. This matches a phone number like 555-1234.
- Match a whole word — Match cat but not category or concat.
- Match a special character literally — Escape characters such as . * + ? ( ) [ ] { } | \ ^ $ with a backslash.
Quantifiers, groups and alternation
- Repeat a pattern — Match between two and four letters a. Also: * zero or more, + one or more, ? optional.
- Match as little as possible — Add ? after a quantifier to make it lazy. This matches the shortest text between the quotes.
- Match one of several options — Match either cat or dog, whichever appears.
- Capture parts of a match — Capture the year, month and day of an ISO date as groups 1, 2 and 3.
- Group without capturing — Group for alternation or repetition without creating a numbered group.
- Name a capture group — Capture a year by name. JavaScript, PCRE and Java use (?<name>...); Python uses (?P<name>...).
- Match repeated text — Match a repeated word such as the the, using a backreference to the first group.
- Ignore case — Match hello in any case. Many tools use an i flag instead.
Lookahead and lookbehind
- Match only if followed by something — Match a number only when it is followed by px, without including px in the match.
- Match only if not followed by something — Match a number that is not followed by px.
- Match only if preceded by something — Match the digits after a dollar sign, without including the sign.
- Match only if not preceded by something — Match a number that does not come right after a dollar sign.
- Require several conditions at once — Use lookaheads at the start to require a lowercase letter, an uppercase letter, a digit and 8 or more characters.
Common patterns
- Email address (simple) — A permissive check for [email protected]. Fully validating email is impractical, so confirm addresses by sending a message.
- Web URL — Match an http or https URL up to the next whitespace.
- IPv4 address (shape only) — Match four groups of 1 to 3 digits. It doesn't check that each is 255 or less.
- ISO date — Match dates like 2026-10-30. It doesn't check that the month or day are valid.
- Hex colour — Match both the short #fff and the long #ffffff forms.
- UUID — Match a UUID in the standard 8-4-4-4-12 format.
- Semantic version — Match versions like 1.4.2, 2.0.0-beta.1 or 1.0.0+build.5.
- URL slug — Match lowercase words joined by single hyphens.
- Hashtag — Match a hash sign followed by word characters.
- Double-quoted string with escapes — Match a quoted string that may contain escaped quotes.
- Trim whitespace — Match leading and trailing whitespace. Replace matches with nothing to trim.
- Collapse repeated spaces — Match two or more whitespace characters, to replace with one space.
Using regex in tools
- Search with grep — Print lines made only of digits. -E enables extended regular expressions.
- Use lookarounds with grep — Print only the matching part, using Perl-compatible syntax with -P (GNU grep).
- Rewrite text with sed — Swap two numbers around a hyphen, using capture groups in the replacement.
- Match in JavaScript — Extract the parts of a date with a regular expression literal.
- Replace in JavaScript — Replace every run of whitespace with a single space. The g flag replaces all matches.
- Find all matches in Python — Return every run of digits in a string. Use raw strings so backslashes aren't treated as escapes.
- Replace in Python — Replace matches using a pattern and a replacement, with \1 style references.
Related cheatsheets
- C++ 23 cheatsheet — Compile and debug C++ with g++ and CMake, and use modern C++ (up to C++23): containers, algorithms, smart pointers, templates and threads.
- C# 14 cheatsheet — Modern C# 14 syntax for strings, nullability, collections, LINQ, records, pattern matching, async code and generics.
- CSS 3 cheatsheet — Layout with flexbox and grid, responsive design, custom properties, animations and modern selectors.
- Go 1 cheatsheet — The go command for modules, building, testing and cross-compiling, plus the code patterns you write every day.
- HTML 5 cheatsheet — HTML5 page structure, text, links, images, forms, tables and metadata, with accessible patterns.
- Java, Maven and Gradle 25 cheatsheet — JDK commands to compile, run and diagnose Java programs, plus the Maven and Gradle commands for builds and tests.