Skip to the content.

Regex Patterns Cheatsheet

A quick-reference guide for Regular Expressions (Regex), covering character classes, anchors, quantifiers, groups, lookaround assertions, regex flags, and common real-world search patterns. ## Character Classes Character classes match specific types of characters. ```regex . # Match any single character except newline (\n) \d # Match any digit (0-9) - equivalent to [0-9] \D # Match any non-digit - equivalent to [^0-9] \w # Match any alphanumeric word character (a-z, A-Z, 0-9, _) \W # Match any non-word character (e.g. spaces, symbols) \s # Match any whitespace character (space, tab, newline, carriage return) \S # Match any non-whitespace character [abc] # Match any single character inside the brackets (a, b, or c) [^abc] # Match any single character NOT inside the brackets (not a, b, or c) [a-z] # Match a character in the range a to z [A-Z] # Match a character in the range A to Z [0-9] # Match a character in the range 0 to 9 [a-zA-Z0-9] # Match any alphanumeric character ``` ## Anchors & Boundaries Anchors assert positions rather than matching characters. ```regex ^ # Assert start of string (or start of line in multi-line mode) $ # Assert end of string (or end of line in multi-line mode) \b # Assert word boundary position (between a word char and a non-word char) \B # Assert non-word boundary position ``` ## Quantifiers Quantifiers specify how many times a character or group must repeat. ```regex * # Match 0 or more times + # Match 1 or more times ? # Match 0 or 1 time (optional) {n} # Match exactly 'n' times {n,} # Match 'n' or more times {min,max} # Match between 'min' and 'max' times (inclusive) # Greedy vs Lazy Matching # By default, quantifiers are greedy (match as much text as possible). Append ? to make them lazy. *? # Match 0 or more times, matching as FEW characters as possible +? # Match 1 or more times, matching as FEW characters as possible ``` ## Grouping & Capturing Groups bundle characters for quantifiers, logic, or extraction. ```regex (abc) # Capturing Group: groups characters and records match for backreference (?:abc) # Non-Capturing Group: groups characters but does NOT record match (saves memory) (?abc) # Named Capturing Group: binds group match to a specific name index \1 # Backreference: matches the exact same text captured by the first group \k # Named Backreference: matches text captured by group named 'name' a|b # Alternation: match 'a' OR 'b' ``` ## Lookaround Assertions Lookarounds assert that a specific pattern exists (or does not exist) without consuming characters in the match (zero-width assertions). ```regex # Lookahead (Look forward/right) (?=abc) # Positive Lookahead: asserts that 'abc' follows immediately (?!abc) # Negative Lookahead: asserts that 'abc' does NOT follow immediately # Lookbehind (Look backward/left) (?<=abc) # Positive Lookbehind: asserts that 'abc' precedes immediately (?

Try in Playground

Practice what you've learned in our interactive browser-based playgrounds: