RegEX CheatSheet

Our Regex Cheat Sheet offers a comprehensive overview of regular expressions, providing a wealth of information and practical tips to help you master the art of pattern matching. Whether you're a beginner just starting to learn about regex or an experienced developer looking for a quick reference guide, this cheat sheet is the ultimate resource for anyone working with regular expressions.

The cheat sheet features an extensive list of regex syntax, including character classes, quantifiers, anchors, groups, and more. Each expression is explained in detail, with clear examples that illustrate how it can be used in practice. In addition, the cheat sheet covers more advanced techniques like lookarounds, backreferences, and non-capturing groups, giving you a complete understanding of how to use regex effectively.

But the cheat sheet isn't just a list of regex syntax - it also includes practical tips and tricks to help you get the most out of your pattern matching. From optimizing your regex patterns for performance to dealing with common pitfalls and errors, the cheat sheet offers valuable insights that will help you work more efficiently and effectively.

Whether you're working on web development, data processing, or any other task that requires pattern matching, our Regex Cheat Sheet is an indispensable resource. With its comprehensive coverage of regex syntax and practical tips for using it effectively, this cheat sheet will help you take your regex skills to the next level and achieve better results in your coding projects.

Table of Content

# Getting Started RegEX

What is RegEX ?

Short for regular expression, a regex is a string of text that allows you to create patterns that help match, locate, and manage text. Perl is a great example of a programming language that utilizes regular expressions. However, it's only one of the many places you can find regular expressions. Regular expressions can also be used from the command line and in-text editors to find text within a file.

Character Classes

Pattern Description
[abc] A single character of: a, b or c
[^abc] A character except: a, b or c
[a-z] A character in the range: a-z
[^a-z] A character not in the range: a-z
[0-9] A digit in the range: 0-9
[a-zA-Z] A character in the range:
a-z or A-Z
[a-zA-Z0-9] A character in the range:
a-z, A-Z or 0-9


Pattern Description
a? Zero or one of a
a* Zero or more of a
a+ One or more of a
[0-9]+ One or more of 0-9
a{3} Exactly 3 of a
a{3,} 3 or more of a
a{3,6} Between 3 and 6 of a
a* Greedy quantifier
a*? Lazy quantifier
a*+ Possessive quantifier

Common Metacharacters

^ { +
< [ *
) > .
( | $
\ ?

Meta Sequences

Pattern Description
. Any single character
\s Any whitespace character
\S Any non-whitespace character
\d Any digit, Same as [0-9]
\D Any non-digit, Same as [^0-9]
\w Any word character
\W Any non-word character
\X Any Unicode sequences, linebreaks included
\C Match one data unit
\R Unicode newlines
\v Vertical whitespace character
\V Negation of \v - anything except newlines and vertical tabs
\h Horizontal whitespace character
\H Negation of \h
\K Reset match
\n Match nth subpattern
\pX Unicode property X
\p{...} Unicode property or script category
\PX Negation of \pX
\P{...} Negation of \p
\Q...\E Quote; treat as literals
\k<name> Match subpattern name
\k'name' Match subpattern name
\k{name} Match subpattern name
\gn Match nth subpattern
\g{n} Match nth subpattern
\g<n> Recurse nth capture group
\g'n' Recurses nth capture group.
\g{-n} Match nth relative previous subpattern
\g<+n> Recurse nth relative upcoming subpattern
\g'+n' Match nth relative upcoming subpattern
\g'letter' Recurse named capture group letter
\g{letter} Match previously-named capture group letter
\g<letter> Recurses named capture group letter
\xYY Hex character YY
\x{YYYY} Hex character YYYY
\ddd Octal character ddd
\cY Control character Y
[\b] Backspace character
\ Makes any character literal


Pattern Description
\G Start of match
^ Start of string
$ End of string
\A Start of string
\Z End of string
\z Absolute end of string
\b A word boundary
\B Non-word boundary


Pattern Description
\0 Complete match contents
\1 Contents in capture group 1
$1 Contents in capture group 1
${foo} Contents in capture group foo
\x20 Hexadecimal replacement values
\x{06fa} Hexadecimal replacement values
\t Tab
\r Carriage return
\n Newline
\f Form-feed
\U Uppercase Transformation
\L Lowercase Transformation
\E Terminate any Transformation

Group Constructs

Pattern Description
(...) Capture everything enclosed
(a|b) Match either a or b
(?:...) Match everything enclosed
(?>...) Atomic group (non-capturing)
(?|...) Duplicate subpattern group number
(?#...) Comment
(?'name'...) Named Capturing Group
(?<name>...) Named Capturing Group
(?P<name>...) Named Capturing Group
(?imsxXU) Inline modifiers
(?(DEFINE)...) Pre-define patterns before using them


- -
(?(1)yes|no) Conditional statement
(?(R)yes|no) Conditional statement
(?(R#)yes|no) Recursive Conditional statement
(?(R&name)yes|no) Conditional statement
(?(?=...)yes|no) Lookahead conditional
(?(?<=...)yes|no) Lookbehind conditional


- -
(?=...) Positive Lookahead
(?!...) Negative Lookahead
(?<=...) Positive Lookbehind
(?<!...) Negative Lookbehind


Pattern Description
g Global
m Multiline
i Case insensitive
x Ignore whitespace
s Single line
u Unicode
X eXtended
U Ungreedy
A Anchor
J Duplicate group names


- -
(?R) Recurse entire pattern
(?1) Recurse first subpattern
(?+1) Recurse first relative subpattern
(?&name) Recurse subpattern name
(?P=name) Match subpattern name
(?P>name) Recurse subpattern name

POSIX Character Classes

Character Class Same as Meaning
[[:alnum:]] [0-9A-Za-z] Letters and digits
[[:alpha:]] [A-Za-z] Letters
[[:ascii:]] [\x00-\x7F] ASCII codes 0-127
[[:blank:]] [\t ] Space or tab only
[[:cntrl:]] [\x00-\x1F\x7F] Control characters
[[:digit:]] [0-9] Decimal digits
[[:graph:]] [[:alnum:][:punct:]] Visible characters (not space)
[[:lower:]] [a-z] Lowercase letters
[[:print:]] [ -~] == [ [:graph:]] Visible characters
[[:punct:]] [!"#$%&()*+,-./:;<=>?@[]^_`{|}~] Visible punctuation characters
[[:space:]] [\t\n\v\f\r ] Whitespace
[[:upper:]] [A-Z] Uppercase letters
[[:word:]] [0-9A-Za-z_] Word characters
[[:xdigit:]] [0-9A-Fa-f] Hexadecimal digits
[[:<:]] [\b(?=\w)] Start of word
[[:>:]] [\b(?<=\w)] End of word

Control verb

- -
(*ACCEPT) Control verb
(*FAIL) Control verb
(*MARK:NAME) Control verb
(*COMMIT) Control verb
(*PRUNE) Control verb
(*SKIP) Control verb
(*THEN) Control verb
(*UTF) Pattern modifier
(*UTF8) Pattern modifier
(*UTF16) Pattern modifier
(*UTF32) Pattern modifier
(*UCP) Pattern modifier
(*CR) Line break modifier
(*LF) Line break modifier
(*CRLF) Line break modifier
(*ANYCRLF) Line break modifier
(*ANY) Line break modifier
\R Line break modifier
(*BSR_ANYCRLF) Line break modifier
(*BSR_UNICODE) Line break modifier
(*LIMIT_MATCH=x) Regex engine modifier
(*LIMIT_RECURSION=d) Regex engine modifier
(*NO_AUTO_POSSESS) Regex engine modifier
(*NO_START_OPT) Regex engine modifier

# Regex examples


Pattern Matches
ring Match ring springboard etc.
. Match a, 9, + etc.
h.o Match hoo, h2o, h/o etc.
dog\? Match dog?
\(hello\) Match (hello)
c:\\Desktop Match c:\Desktop


Pattern Matches
apple|banana Match apple or banana
id|identity Match id or identity
identity|id Match id or identity

Character classes

Pattern Matches
[aeiou] Match any vowel
[^aeiou] Match a NON vowel
r[iau]ng Match ring, wrangle, sprung, etc.
gr[ae]y Match gray or grey
[a-zA-Z0-9] Match any letter or digit

Shorthand classes

Pattern Meaning
\w "Word" character
(letter, digit, or underscore)
\d Digit
\s Whitespace
(space, tab, vtab, newline)
\W, \D, or \S Not word, digit, or whitespace
[\D\S] Means not digit or whitespace, both match
[^\d\s] Disallow digit and whitespace


Pattern Matches
colou?r Match color or colour
[BW]ill[ieamy's]* Match Bill, Willy, William's etc.
[a-zA-Z]+ Match 1 or more letters
\d{3}-\d{2}-\d{4} Match a SSN
[a-z]\w{1,7} Match a UW NetID

# Python RegEX

Python RegEX Module

Python has a built-in package called re, which can be used to work with Regular Expressions.

Import the re module:

        import re


Function Description
re.findall Returns a list containing all matches
re.finditer Return an iterable of match objects (one for each match) Returns a Match object if there is a match anywhere in the string
re.split Returns a list where the string has been split at each match
re.sub Replaces one or many matches with a string
re.compile Compile a regular expression pattern for later use
re.escape Return string with all non-alphanumerics backslashed


- - -
re.I re.IGNORECASE Ignore case
re.M re.MULTILINE Multiline
re.L re.LOCALE Make \w,\b,\s locale dependent
re.S re.DOTALL Dot matches all (including newline)
re.U re.UNICODE Make \w,\b,\d,\s unicode dependent
re.X re.VERBOSE Readable style

Python Examples

>>> sentence = 'This is a sample string'
>>> bool('this', sentence, flags=re.I))
>>> bool('xyz', sentence))


>>> re.findall(r'\bs?pare?\b', 'par spar apparent spare part pare')
['par', 'spar', 'spare', 'pare']
>>> re.findall(r'\b0*[1-9]\d{2,}\b', '0501 035 154 12 26 98234')
['0501', '154', '98234']


>>> m_iter = re.finditer(r'[0-9]+', '45 349 651 593 4 204')
>>> [m[0] for m in m_iter if int(m[0]) < 350]
['45', '349', '4', '204']


>>> re.split(r'\d+', 'Sample123string42with777numbers')
['Sample', 'string', 'with', 'numbers']


>>> ip_lines = "catapults\nconcatenate\ncat"
>>> print(re.sub(r'^', r'* ', ip_lines, flags=re.M))
* catapults
* concatenate
* cat


>>> pet = re.compile(r'dog')
>>> type(pet)

>>> bool('They bought a dog'))
>>> bool('A cat crossed their path'))

# JavaScript RegEX


const str = 'table football';

const regex = new RegExp('foo*');
const globalRegex = new RegExp('foo*', 'g');

// Output: true

// Output: 0

// Output: true

// Output: 9

// Output: false
let text = 'I like APPles very much';
let regexA = /apples/;
let regexB = /apples/i;

// Output: -1

// Output: 7


let text = 'Do you like apples?';
let regex= /apples/;

// Output: apples 

// Output: Do you like apples?


const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
const regex = /[A-Z]/g;
const found = paragraph.match(regex);

// Output: Array ["T", "I"]


let text = 'This 593 string will be brok294en at places where d1gits are.';
let regex = /\d+/g
// Put Result: [ "This ", " string will be brok", "en at places where d", "gits are." ]

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split(' ');
// Output: "fox"

const chars = str.split('');
// Output: "k"

const strCopy = str.split();
// Output: Array ["The quick brown fox jumps over the lazy dog."]


let regex = /t(e)(st(\d?))/g;
let text = 'test1test2';
let array = [...text.matchAll(regex)];

// Output: ["test1", "e", "st1", "1"]

// Output: ["test2", "e", "st2", "2"]


let text = 'Do you like aPPles?';
let regex = /apples/i

let result = text.replace(regex, 'mangoes');
// Output: Do you like mangoes?


const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
console.log(p.replaceAll('dog', 'monkey'));
// Output: "The quick brown fox jumps over the lazy monkey. If the monkey reacted, was it really lazy?"

// global flag required when calling replaceAll with regex
const regex = /Dog/ig;
console.log(p.replaceAll(regex, 'ferret'));
// Output: "The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?"

let regex = /apples/gi;
let text = 'Here are apples and apPleS';
let result = text.replaceAll(regex, "mangoes");
// Output: Here are mangoes and mangoes



- -
preg_match() Performs a regex match
preg_match_all() Perform a global regular expression match
preg_replace_callback() Perform a regular expression search and replace using a callback
preg_replace() Perform a regular expression search and replace
preg_split() Splits a string by regex pattern
preg_grep() Returns array entries that match a pattern


$str = "Visit Samsung!";
$regex = "/samsung/i";

echo preg_replace($regex, "", $str); 
// Output: Visit!


$str = "Visit cheatsheet";
$regex = "#cheatsheet#i";
echo preg_match($regex, $str);
// Output: 1

// Declare a variable and initialize it 
$str = "Check For Testing."; 
// case-Insensitive search for the word "Check" 
if (preg_match("/\bCheck\b/i", $str, $match))  
    echo "Matched String!"; 
    echo "Not matched for String";   
// Output : Matched String!


<!-- Example 1 -->
$regex = "/[a-zA-Z]+ (\d+)/";
$input_str = "June 24, August 13, and December 30";
if (preg_match_all($regex, $input_str, $matches_out)) {

    echo count($matches_out);
    // Output: 2

    echo count($matches_out[0]);
    // Output: 3

    // Output: Array("June 24", "August 13", "December 30")

    // Output: Array("24", "13", "30")

<!-- Example 2 -->
$str = "abc ABC";
$pattern = "/((a)b)(c)/i";
if(preg_match_all($pattern, $str, $matches, PREG_PATTERN_ORDER)) {


$arr = ["Jane", "jane", "Joan", "JANE"];
$regex = "/Jane/";

echo preg_grep($regex, $arr);
// Output: Jane

$input = ["Red", "Pink", "Green", "Blue", "Purple"];
$result = preg_grep("/^p/i", $input);
// Output: 
    Array (
        [1] => Pink
        [4] => Purple


<!-- Example 1 -->
$str = "Jane\tKate\nLucy Marion";
$regex = "@\s@";

print_r(preg_split($regex, $str));
// Output: Array("Jane", "Kate", "Lucy", "Marion")

<!-- Example 2 -->
$ip = "123.456.789.000"; // some IP address
$iparr = preg_split ("/\./", $ip); 

print "$iparr[0] <br />";
print "$iparr[1] <br />" ;
print "$iparr[2] <br />"  ;
print "$iparr[3] <br />"  ;
// Output:

# Java RegEX


// The First way to use
Pattern p = Pattern.compile(".s", Pattern.CASE_INSENSITIVE);
Matcher ma = p.matcher("aS");  
boolean firstWay = m.matches();  
// Outputs: true

// The Second way to use
boolean secondWay = Pattern.compile(".s").matcher("as").matches();  
// Outputs: true

// The Third way to use
boolean thirdWay = Pattern.matches(".s", "BBBBB");
// Output: false

Pattern Fields

- -
CANON_EQ Canonical equivalence
CASE_INSENSITIVE Case-insensitive matching
COMMENTS Permits whitespace and comments
DOTALL Dotall mode
MULTILINE Multiline mode
UNICODE_CASE Unicode-aware case folding
UNIX_LINES Unix lines mode

Pattern Method

  • Pattern compile(String regex [, int flags])
  • boolean matches([String regex, ] CharSequence input)
  • String[] split(String regex [, int limit])
  • String quote(String s)

Matcher Method

  • int start([int group | String name])
  • int end([int group | String name])
  • boolean find([int start])
  • String group([int group | String name])
  • Matcher reset()

String Method

  • boolean matches(String regex)
  • String replaceAll(String regex, String replacement)
  • String[] split(String regex[, int limit])


// Replace sentence:
String regex = "[A-Z\n]{5}$";
String str = "I love APP\nLE";

Pattern p = Pattern.compile(regex, Pattern.MULTILINE);
Matcher m = p.matcher(str);
// Outputs: I love Apple!

// Array of all matches:
String str = "She sells seashells by the Seashore";
String regex = "\\w*se\\w*";

Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(str);

List matches = new ArrayList<>();
while (m.find()) {
// Outputs: [sells, seashells, Seashore]

// returns true if the string contains of three letters
System.out.println(Pattern.matches("[a-zA-Z][a-zA-Z][a-zA-Z]", "aPz")); 
System.out.println(Pattern.matches("[a-zA-Z][a-zA-Z][a-zA-Z]", "aAA"));
System.out.println(Pattern.matches("[a-zA-Z][a-zA-Z][a-zA-Z]", "apZx"));
// Output: true true false

boolean match1 = Pattern.compile("lin.x").matcher("linux").matches();
boolean match2 = Pattern.matches("clo..", "cloud");
// Output: true true

Best Suggest