logo
eng-flag

Regex Cheatsheet

Table of Contents

  1. Basic Matchers
  2. Meta Characters
  3. Quantifiers
  4. Character Classes
  5. Groups and Capturing
  6. Anchors
  7. Lookaround
  8. Flags
  9. Common Patterns
  10. Special Characters

Basic Matchers

hello               # Matches the string "hello"
hello world         # Matches the string "hello world"

Meta Characters

.                   # Matches any character except newline
^                   # Matches start of line
$                   # Matches end of line
*                   # Matches 0 or more occurrences
+                   # Matches 1 or more occurrences
?                   # Matches 0 or 1 occurrence
                   # Escapes special characters
|                   # Alternation (OR operator)

Quantifiers

*                   # 0 or more
+                   # 1 or more
?                   # 0 or 1
{3}                 # Exactly 3
{3,}                # 3 or more
{3,5}               # Between 3 and 5

Character Classes

[abc]               # Matches a, b, or c
[^abc]              # Matches any character except a, b, or c
[a-z]               # Matches any lowercase letter
[A-Z]               # Matches any uppercase letter
[0-9]               # Matches any digit
[a-zA-Z0-9]         # Matches any alphanumeric character

Groups and Capturing

(abc)               # Capturing group
(?:abc)             # Non-capturing group
\1                  # Backreference to group #1
(?<name>abc)        # Named capturing group
(?P<name>abc)       # Named capturing group (Python syntax)

Anchors

^                   # Start of line
$                   # End of line
                  # Word boundary
B                  # Not a word boundary

Lookaround

(?=abc)             # Positive lookahead
(?!abc)             # Negative lookahead
(?<=abc)            # Positive lookbehind
(?<!abc)            # Negative lookbehind

Flags

i                   # Case-insensitive
g                   # Global
m                   # Multi-line
s                   # Dot matches newline
u                   # Unicode
y                   # Sticky

Common Patterns

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$  # Email
^(?=.*[A-Za-z])(?=.*d)[A-Za-zd]{8,}$            # Password (min 8 chars, at least one letter and one number)
^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$  # IPv4 address
^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?.)+[a-zA-Z]{2,}$  # Domain name
^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$               # Hex color code

Special Characters

d                  # Digit [0-9]
D                  # Not a Digit [^0-9]
w                  # Word Character [a-zA-Z0-9_]
W                  # Not a Word Character [^a-zA-Z0-9_]
s                  # Whitespace [ 	

]
S                  # Not Whitespace [^ 	

]

Examples

# Match a phone number
^+?(d{1,3})?[-.s]?(?d{3})?[-.s]?d{3}[-.s]?d{4}$

# Match a date (MM/DD/YYYY or MM-DD-YYYY)
^(0[1-9]|1[0-2])[/.-](0[1-9]|[12][0-9]|3[01])[/.-](19|20)dd$

# Match a URL
^(https?://)?([da-z.-]+).([a-z.]{2,6})([/w .-]*)*/?$

# Match a credit card number
^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35d{3})d{11})$

# Match a ISBN-10 or ISBN-13 number
^(?:ISBN(?:-1[03])?:? )?(?=[0-9X]{10}$|(?=(?:[0-9]+[- ]){3})[- 0-9X]{13}$|97[89][0-9]{10}$|(?=(?:[0-9]+[- ]){4})[- 0-9]{17}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9X]$

2024 © All rights reserved - buraxta.com