Skip to content

🧠 A practical, language-agnostic, copy-paste-ready cheat sheet of real-world RegExp patterns, organized by category. From emails and dates to URLs, binary formats, colors, and beyond β€” no theory, just patterns that work.

License

Notifications You must be signed in to change notification settings

anthonyamar/regex-cookbook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 

Repository files navigation

Regex Cookbook – Copyable Patterns for Real-World Use

Who likes writing regex? No one. Who needs regex? Everyone!

So stop googling regex. Start pasting them. 🧠

Regex Cookbook is a practical, language-agnostic, copy-paste-ready cheat sheet of real-world RegExp patterns, organized by category. From emails and dates to URLs, binary formats, colors, and beyond β€” no theory, just patterns that work.

Index

🧱 Basics

Match numbers

/\d+/

Matches one or more digits.

  • βœ… 123
  • βœ… 007
  • ❌ abc

Match lowercase letters

/[a-z]+/

Matches one or more lowercase English letters.

  • βœ… hello
  • ❌ HELLO
  • ❌ Hello123

Match uppercase letters

/[A-Z]+/

Matches one or more uppercase English letters.

  • βœ… ABC
  • βœ… HELLO
  • ❌ Hello
  • ❌ abc

Match punctuation

/[.,;:!?'"(){}\[\]-]+/

Matches one or more punctuation characters.

  • βœ… ,!?
  • βœ… (hello)
  • ❌ hello

Match any Unicode or accented character

/[\p{L}]/u

Matches any letter from any language (including accents and scripts like Arabic, Cyrillic, etc.).

  • βœ… Γ©
  • βœ… Γ§
  • βœ… Γ¦
  • βœ… Ψ£
  • ❌ 123

Requires the Unicode flag /u.


Match emojis

/[\p{Emoji}]/u

Matches any emoji character.

  • βœ… 😊
  • βœ… πŸ’‘
  • βœ… πŸ„β€β™‚οΈ
  • ❌ Hello

Requires modern engines (JS ES2018+). Not all engines support \p{Emoji}.


Match specific word or phrase

/\b(hello|hi|hey)\b/i

Matches exactly β€œhello”, β€œhi”, or β€œhey” as whole words, case-insensitive.

  • βœ… hello
  • βœ… Hi
  • βœ… HEY
  • ❌ helloo
  • ❌ ohhey

Add or remove words inside (word1|word2|...) to adjust.

πŸ“… Date

Date in format YYYY-MM-DD

/^\d{4}-\d{2}-\d{2}$/

Matches a date formatted as year-month-day (ISO 8601).

  • βœ… 2023-12-25
  • βœ… 1999-01-01
  • ❌ 12-25-2023
  • ❌ 2023/12/25

Change - to / or . if your date format uses different separators.


Date in format MM/DD/YYYY

/^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$/

Matches dates like month/day/year.

  • βœ… 12/25/2023
  • βœ… 01/01/2000
  • ❌ 25/12/2023
  • ❌ 1/1/2000

This forces 2-digit month and day. Use \/? if the slash is optional.

πŸ•’ Time

24-hour time (HH:MM)

/^([01]\d|2[0-3]):[0-5]\d$/

Matches a valid time in 24-hour format.

  • βœ… 09:30
  • βœ… 23:59
  • ❌ 24:00
  • ❌ 9:5

To allow optional leading zeros, change [01]\d to \d{1,2}.


12-hour time with AM/PM

/^(0?[1-9]|1[0-2]):[0-5]\d\s?(AM|PM)$/i

Matches 12-hour time with AM or PM suffix.

  • βœ… 10:45 AM
  • βœ… 1:30 pm
  • ❌ 13:00 PM
  • ❌ 10:75 AM

The i at the end makes it case-insensitive.

⌚ DateTime

ISO 8601 datetime

/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[\+\-]\d{2}:\d{2})$/

Full ISO datetime with timezone.

  • βœ… 2023-10-12T14:48:00Z
  • βœ… 2023-10-12T14:48:00+02:00
  • ❌ 2023-10-12 14:48:00

Match Unix timestamp (10 digits)

/^\d{10}$/

Matches a 10-digit timestamp in seconds.

  • βœ… 1617181723
  • ❌ 161718 (too short)
  • ❌ 2023-01-01

πŸ“§ Email

Basic email pattern

/^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$/

Matches standard email addresses.

To restrict domains (e.g. only .com), replace \.[a-zA-Z]{2,} with \.com.

πŸ”’ Password

Complex password

/^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*\W*)(?!.*\s).{8,16}$/

Accepts at least one number, one uppercase letter, one lowercase letter, and zero or more special characters, with a length between 8 to 16 characters.

  • βœ… Password1?
  • βœ… *pwD123!:?+
  • ❌ password
  • ❌ my super secure password

Change the numbers in {8,16} to adjust length. Use {8,} for "8 or more characters".

πŸ“± Phone Numbers

Basic international phone number

/^\+\d{1,3}[\s.-]?\(?\d+\)?[\s.-]?\d+[\s.-]?\d+$/

Matches common international formats.

  • βœ… +33 6 12 34 56 78
  • βœ… +1 (123) 456-7890
  • ❌ 06.12.34.56.78 (without +)

Match international prefix (e.g. +33, +1, +867)

/^\+(\d{1,4})/

Extracts the international prefix from a phone number starting with +.

  • βœ… +33 6 12 34 56 78 β†’ captures 33
  • βœ… +1 (123) 456-7890 β†’ captures 1
  • ❌ 0033 6 12 34 56 78

Match phone numbers with either + or 00 prefix

/^(?:\+|00)\d{1,4}[\s.-]?\d+([\s.-]?\d+)*$/

Matches international numbers that start with either + or 00, followed by digits.

  • βœ… +33 6 12 34 56 78
  • βœ… 0033 6 12 34 56 78
  • βœ… +1-123-456-7890
  • ❌ 06 12 34 56 78

Use (?:\+|00) to support both styles for international dialing.

🌐 URLs

Generic URL (http/https with subdomains and path)

/https?:\/\/([\w.-]+)\.[a-z]{2,}(\/[\w\-._~:\/?#[\]@!$&'()*+,;=]*)?/i

Matches URLs with http or https, optional subdomain and path.

To allow ports (e.g. :3000), add (:\d+)? after the domain.


Domain only (no protocol, no path)

/^[\w-]+\.[a-z]{2,}$/

Matches only the domain.


HTTPS domain only (no subdomain)

/^https:\/\/[a-z0-9-]+\.[a-z]{2,}$/

Matches only https full domains without subdomains.


FTP URL

/^ftp:\/\/[\w.-]+\.[a-z]{2,}(\/.*)?$/

Matches FTP links with optional path.

  • βœ… ftp://files.example.com
  • βœ… ftp://example.com/folder/file.txt
  • ❌ http://example.com

IP address with http/https

/^https?:\/\/(\d{1,3}\.){3}\d{1,3}(\/.*)?$/

Matches an IP-based URL.


Localhost with port

/^https?:\/\/localhost:\d{2,5}(\/.*)?$/

Matches localhost with port and optional path.

πŸ“œ File Extensions

File with extension

/^[\w,\s-]+\.[A-Za-z]{2,4}$/

Basic filename with extension.

  • βœ… file.txt
  • βœ… my photo.jpeg
  • ❌ file

Image file

/^.+\.(jpg|jpeg|png|gif|bmp|svg)$/i

Common image formats.

  • βœ… logo.png
  • βœ… photo.JPG
  • ❌ file.pdf

PDF file

/^.+\.pdf$/i
  • βœ… document.pdf
  • ❌ document.docx

🧬 Syntax

flatcase

/^[a-z]+$/

All lowercase, no separator.

  • βœ… hello
  • ❌ Hello
  • ❌ helloWorld

UPPERFLATCASE

/^[A-Z]+$/

All uppercase, no separator.

  • βœ… HELLO
  • ❌ Hello
  • ❌ HELLO_WORLD

camelCase

/^[a-z]+(?:[A-Z][a-z]*)+$/

Starts lowercase, words joined with uppercase letters.

  • βœ… helloWorld
  • ❌ HelloWorld
  • ❌ hello_world

UpperCamelCase (PascalCase)

/^(?:[A-Z][a-z]+)+$/

Each word starts with a capital letter.

  • βœ… HelloWorld
  • ❌ helloWorld
  • ❌ Hello_World

snake_case

/^[a-z]+(_[a-z]+)+$/

Lowercase words separated by underscores.

  • βœ… hello_world
  • βœ… foo_bar_baz
  • ❌ Hello_World
  • ❌ helloWorld

SCREAMING_SNAKE_CASE

/^[A-Z]+(_[A-Z]+)+$/

All uppercase with underscores.

  • βœ… HELLO_WORLD
  • ❌ Hello_World
  • ❌ hello_world

Camel_Snake_Case

/^([A-Z][a-z]+_)+[A-Z][a-z]+$/

PascalCase segments separated by underscores.

  • βœ… Hello_World_Example
  • ❌ hello_world
  • ❌ HelloWorld

kebab-case

/^[a-z]+(-[a-z]+)+$/

Lowercase words separated by hyphens.

  • βœ… hello-world
  • ❌ Hello-World
  • ❌ helloWorld

SCREAMING-KEBAB-CASE

/^[A-Z]+(-[A-Z]+)+$/

Uppercase words separated by hyphens.

  • βœ… HELLO-WORLD
  • ❌ hello-world

Train-Case

/^([A-Z][a-z]+-)+[A-Z][a-z]+$/

PascalCase segments separated by hyphens.

  • βœ… Hello-World-Example
  • ❌ HelloWorld
  • ❌ hello-world

πŸ’» HTML

Match content inside HTML tags

/>([^<]+)</

Captures the content between tags.

  • βœ… <b>Hello</b> β†’ Hello
  • ❌ <b></b>

Match full HTML tag with content

/<(\w+)([^>]*)>(.*?)<\/\1>/

Captures entire tag with content.

  • βœ… <div class="x">Hello</div>
  • ❌ <img src="x.jpg"/>

Match self-closing tag

/<\w+[^>]*\/>/

Matches self-closing HTML tags.

  • βœ… <img src="image.jpg" />
  • βœ… <br/>
  • ❌ <div>Hello</div>

Match HTML comment

/<!--[\s\S]*?-->/

Matches HTML comments.

  • βœ… <!-- This is a comment -->

HTML tag pair without nesting

/<(\w+)[^>]*>([^<]*)<\/\1>/

Simple tag pair with content, not nested.

  • βœ… <b>Hello</b>
  • ❌ <b><i>Bold</i></b>

🎨 Colors

Hexadecimal color (#RGB or #RRGGBB)

/^#(?:[0-9a-fA-F]{3}){1,2}$/

Matches 3- or 6-digit hexadecimal color codes.

  • βœ… #fff
  • βœ… #FFFFFF
  • βœ… #123abc
  • ❌ #abcd
  • ❌ 123456

rgb(r, g, b)

/^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/

Matches an rgb() color with values 0–255.

  • βœ… rgb(255, 0, 127)
  • βœ… rgb( 0 , 255 , 64 )
  • ❌ rgb(300,0,0)
  • ❌ rgba(255,0,0,0.5)

rgba(r, g, b, a)

/^rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(0|1|0?\.\d+)\s*\)$/

Matches an rgba() color with alpha 0–1.

  • βœ… rgba(255, 0, 127, 0.5)
  • βœ… rgba(0, 0, 0, 1)
  • ❌ rgba(0, 0, 0)
  • ❌ rgba(255,255,255,1.5)

hsl(hue, sat%, light%)

/^hsl\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*\)$/

Matches hsl() values where hue is 0–360, sat/light are percentages.

  • βœ… hsl(120, 100%, 50%)
  • βœ… hsl(0,0%,0%)
  • ❌ hsl(400,100,50)
  • ❌ hsl(120, 50, 50)

oklch(L C H)

/^oklch\(\s*([\d.]+)\s+([\d.]+)\s+([\d.]+)(?:\s*\/\s*([\d.]+))?\s*\)$/

Matches OKLCH format with optional alpha (/0.5).

  • βœ… oklch(0.628 0.12 281.07)
  • βœ… oklch(0.628 0.12 281.07 / 0.5)
  • ❌ oklch(0.628, 0.12, 281.07)
  • ❌ oklch(0.6 0.2)

cmyk(c, m, y, k)

/^cmyk\(\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*\)$/

Matches CMYK color values with percentages for cyan, magenta, yellow, and black.

  • βœ… cmyk(0%, 100%, 100%, 0%)
  • βœ… cmyk(25%, 10%, 0%, 80%)
  • ❌ cmyk(100, 0, 0, 0) (missing %)
  • ❌ cmyk(0%, 0%, 0%)

Pantone code (e.g. Pantone 300 C)

/^Pantone\s?(\d{3,4})(\s?[A-Z]{1,2})?$/

Matches Pantone codes with optional suffix like C, U, etc.

  • βœ… Pantone 300
  • βœ… Pantone 300 C
  • βœ… Pantone 485U
  • ❌ Pantone red
  • ❌ Pantone 30A

RAL Classic (e.g. RAL 3020)

/^RAL\s?(\d{4})$/

Matches classic RAL color codes.

  • βœ… RAL 3020
  • βœ… RAL3003
  • ❌ RAL red
  • ❌ RAL 123

πŸ’‘ You can extend this to RAL Design (e.g. RAL 210 50 20) with:

/^RAL\s(\d{3})\s(\d{2})\s(\d{2})$/
  • βœ… RAL 210 50 20
  • ❌ RAL 2105020
  • ❌ RAL 210-50-20

πŸ“¦ Data Structures

Match basic JSON object

/^\{\s*"[^"]+"\s*:\s*.+\}$/

Matches a flat JSON object with a single key-value pair.

  • βœ… {"name": "Jean"}
  • ❌ {"name": "Jean", "age": 30} (multi-pair, nested)
  • ❌ name: "Jean"

Use a proper parser for nested or valid JSON.


Match XML tag with content

/<(\w+)[^>]*>(.*?)<\/\1>/

Matches any XML element with opening and closing tags.

  • βœ… <data>Hello</data>
  • βœ… <tag attr="x">Value</tag>
  • ❌ <data />

Match YAML key-value line

/^\s*[\w.-]+\s*:\s*.+$/

Matches a basic YAML key and value on a single line.

  • βœ… name: Jean
  • βœ… user_id: 12345
  • ❌ - item: value (list format)
  • ❌ name "Jean"

🌍 IPs

IPv4

/^(\d{1,3}\.){3}\d{1,3}$/

Matches standard IPv4 format.

  • βœ… 192.168.0.1
  • βœ… 8.8.8.8
  • ❌ 999.999.999.999 (invalid range, but matches)

For stricter check, validate each octet is <= 255 programmatically.


IPv4 with port

/^(\d{1,3}\.){3}\d{1,3}:\d+$/

Matches an IPv4 address followed by a port (e.g. :8080).

  • βœ… 127.0.0.1:3000
  • βœ… 192.168.1.10:80
  • ❌ 192.168.1.10
  • ❌ localhost:3000

Match IPv4 CIDR block

/^(\d{1,3}\.){3}\d{1,3}\/\d{1,2}$/

Matches IPv4 blocks in CIDR notation.

  • βœ… 192.168.0.0/24
  • βœ… 10.0.0.0/8
  • ❌ 192.168.0.1
  • ❌ /24

IPv6

/^([a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/

Matches full-form IPv6 addresses.

  • βœ… 2001:0db8:85a3:0000:0000:8a2e:0370:7334
  • ❌ ::1 (shortened form not supported by this regex)

🧩 Text

No unicode characters (letters, numbers, basic punctuation)

/^[\x00-\x7F]+$/

Accepts only basic ASCII characters.

  • βœ… Bank Statement 10
  • ❌ DΓ©claration bancaire
  • ❌ StatementπŸ’Έ

No unicode + no digits

/^[A-Za-z\s.,;:!?'"()-]+$/

Only allows ASCII letters and punctuation.

  • βœ… Bank Statement
  • ❌ Statement 10
  • ❌ RΓ©sumΓ©

No emojis

/^(?!.*[\u{1F600}-\u{1F64F}]).*$/u

Rejects any string containing emojis in the emoticon range.

  • βœ… Hello world
  • ❌ Hello 😊

Use different unicode ranges for other emoji types.


πŸ”’ Numbers

Only digits

/^\d+$/

Only numbers.

  • βœ… 123456
  • ❌ 123 456
  • ❌ 12a34

Decimal with dot

/^\d+(\.\d+)?$/

Matches numbers with optional decimal.

  • βœ… 12
  • βœ… 12.34
  • ❌ 12,34

To use comma instead of dot, replace \. with ,.


Positive or negative integer

/^-?\d+$/

Matches whole numbers, including negative ones.

  • βœ… 42
  • βœ… -15
  • ❌ 3.14
  • ❌ 1,000

Decimal with optional minus sign

/^-?\d+(\.\d+)?$/

Matches decimal numbers with optional minus sign.

  • βœ… -12.34
  • βœ… 5.0
  • βœ… 42
  • ❌ 5,0

Two decimals (basic, no symbols)

/^\d+(?:\.\d{2})?$/

Matches prices with optional 2 decimal places.

  • βœ… 10
  • βœ… 10.00
  • ❌ 10.0
  • ❌ 10.000

Change \. to , for locales using comma separators.

Negative numbers in accounting style with parentheses

/^\(\d{1,3}(?:[.,]?\d{3})*(?:[.,]\d+)?\)$/

Matches negative numbers written in parentheses, like (1,234.56) or (1234,56). Accepts both . and , for decimal or thousand separators.

  • βœ… (123)
  • βœ… (1,234.56)
  • βœ… (1234,56)
  • βœ… (1.234,99)
  • ❌ -123
  • ❌ ( 123 )
  • ❌ (1234.567.89)

πŸ’‘ To force dot or comma for decimal, replace [.,] by \. or ,. πŸ’‘ If you don’t use thousands separators, simplify to:

/^\(\d+(?:[.,]\d+)?\)$/

πŸ’° Currency & Symbols

Match currency symbol only

/[\$\€\Β£\Β₯\β‚½\β‚Ή]/

Matches standalone currency symbols.

  • βœ… $
  • βœ… €
  • βœ… Β£
  • βœ… Β₯
  • ❌ 100
  • ❌ 10€

Add or remove symbols inside the brackets to customize.


Match currency with leading symbol (e.g. $100, Β£ 100.83)

/[\$\€\Β£\Β₯\β‚½\β‚Ή]\s?\d+(?:[.,]\d{1,2})?/

Matches currencies like $100, Β£ 100.83, with or without space between symbol and number. Accepts comma or dot as decimal.

  • βœ… $100
  • βœ… € 100,50
  • βœ… Β£99.99
  • ❌ 100€

Change [.,] to force one decimal separator. Use [.,]\d{2} to force 2 decimals.


Match currency with trailing symbol (e.g. 100€, 1.500,00 €)

/\d+(?:[.,]\d{1,2})?\s?[\$\€\Β£\Β₯\β‚½\β‚Ή]/

Matches formats like 100€, 1.500,00 €, with optional space.

  • βœ… 10€
  • βœ… 99.99€
  • βœ… 1 000,00 €
  • ❌ $100

Match percentage (e.g. 20%, 20 %, 20.5%, 20,5 %)

/\d+(?:[.,]\d+)?\s?%/

Matches percentages with or without decimal and space.

  • βœ… 20%
  • βœ… 20 %
  • βœ… 20.5%
  • βœ… 20,5 %
  • ❌ %20

πŸ’³ Finance

Match IBAN (general structure)

/^[A-Z]{2}\d{2}[A-Z0-9]{11,30}$/

Matches IBAN format (country code, checksum, BBAN).

  • βœ… FR7630006000011234567890189
  • βœ… DE89370400440532013000
  • ❌ FR76 3000 6000 0112... (spaces)

Remove spaces before testing. Specific formats vary by country.


Match BIC/SWIFT

/^[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$/

Matches standard 8 or 11-character SWIFT/BIC codes.

  • βœ… BNPAFRPP
  • βœ… DEUTDEFF500
  • ❌ BICFRPP123456

Match credit card number (basic)

/^\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}$/

Matches 16-digit credit card number with optional spaces or dashes.

  • βœ… 4111 1111 1111 1111
  • βœ… 4111-1111-1111-1111
  • βœ… 4111111111111111
  • ❌ 4111-1111-1111

This does not validate via the Luhn algorithm.


πŸ“Š Separated values formats

CSV (comma-separated values)

/^([^,\n]+,)*[^,\n]+$/

Matches comma-separated values on one line.

  • βœ… a,b,c
  • ❌ a,,c
  • ❌ a,b,\nc

TSV (tab-separated values)

/^([^\t\n]+\t)*[^\t\n]+$/

Matches tab-separated values on one line.

  • βœ… a\tb\tc
  • ❌ a\t\tc

Semi-colon-separated values

/^([^;\n]+;)*[^;\n]+$/

Matches semicolon-separated values.

  • βœ… a;b;c
  • ❌ a;;c
  • ❌ a;b;\nc

πŸ”– Slugs & Identifiers

URL slug

/^[a-z0-9]+(?:-[a-z0-9]+)*$/

Lowercase, hyphenated, SEO-friendly string.

  • βœ… hello-world
  • βœ… my-article-123
  • ❌ Hello_World
  • ❌ hello--world

UUID v4

/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i

Matches a version 4 UUID.

  • βœ… 550e8400-e29b-41d4-a716-446655440000
  • ❌ 550e8400e29b41d4a716446655440000

🧩 Shortcodes & Custom Placeholders

Match content inside < > (e.g. <shortcode-identifier>)

/<([^<>]+)>/

Matches anything between < and >, excluding nested or malformed tags.

  • βœ… <shortcode>
  • βœ… <user_name>
  • ❌ <>
  • ❌ <shortcode

πŸ”§ How to adapt to other symbols

Match (shortcode)
/\(([^()]+)\)/
Match [shortcode]
/\[([^\[\]]+)\]/
Match {shortcode}
/\{([^{}]+)\}/
Match {{shortcode}} (double braces)
/\{\{([^{}]+)\}\}/
Match <-shortcode->
/<\-([^<>]+)\->/
  • [^...] ensures the content does not include the delimiters themselves.
  • Use g (global flag) if you want to match multiple shortcodes in one string.
  • Escape characters like (, [, { using \ inside the regex.

πŸ” Repetition & Structure

Repeated character

/(.)\1{2,}/

Matches 3 or more of the same character in a row.

  • βœ… aaa
  • βœ… 1111
  • ❌ ababab
  • ❌ aa

Change {2,} to {N-1,} to match N repetitions. Example: {6,} matches 7 or more.

πŸ–₯️ Binary & Low-Level Patterns

Binary octets (8 bits each)

/([01]{8})/g

Matches binary data in groups of 8 bits (octets).

  • βœ… 0101010101010101 β†’ 01010101, 01010101
  • βœ… 1111000010101010
  • ❌ 0101010 (only 7 bits)

Use with the global flag /g to match multiple octets in a stream.


MAC address

/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/

Matches standard MAC addresses with : or - as separator.

  • βœ… 00:1A:2B:3C:4D:5E
  • βœ… 00-1A-2B-3C-4D-5E
  • ❌ 001A2B3C4D5E
  • ❌ 00:1A:2B:3C:4D

Hexadecimal number (without #)

/\b(?:0x)?[0-9a-fA-F]+\b/

Matches raw hex numbers, optionally starting with 0x, but no #.

  • βœ… 0x1A3F
  • βœ… a4f3
  • βœ… DEADBEAF
  • ❌ #FF00FF

File size strings (e.g. 5.6 MB)

/^\d+(\.\d+)?\s?(B|KB|MB|GB|TB)$/i

Matches common file size formats.

  • βœ… 100KB
  • βœ… 5.6 MB
  • βœ… 2048B
  • ❌ 5,6MB (comma instead of dot)

Windows file path

/^[A-Z]:\\(?:[^\\\/:*?"<>|\r\n]+\\)*[^\\\/:*?"<>|\r\n]*$/i

Matches a full Windows-style path.

  • βœ… C:\Users\MyFolder\File.txt
  • βœ… D:\Backup\2024\Logs\log.txt
  • ❌ /usr/local/bin

Unix file path

/^\/(?:[^\/\n]+\/)*[^\/\n]+$/

Matches a Unix-like full path.

  • βœ… /usr/local/bin/python
  • βœ… /var/log/nginx/access.log
  • ❌ C:\Program Files

πŸ” Cryptography

Base64-encoded string

/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/

Matches a valid Base64-encoded block.

  • βœ… TWFu
  • βœ… U29tZSBkYXRhIHdpdGggZXF1YWwgcGFkZGluZw==
  • ❌ This is not base64

MD5 hash (32 hex chars)

/^[a-f0-9]{32}$/i
  • βœ… d41d8cd98f00b204e9800998ecf8427e
  • ❌ too_short_hash

SHA1 hash (40 hex chars)

/^[a-f0-9]{40}$/i
  • βœ… da39a3ee5e6b4b0d3255bfef95601890afd80709
  • ❌ 123abc

SHA256 hash (64 hex chars)

/^[a-f0-9]{64}$/i
  • βœ… e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
  • ❌ short_hash

JWT token format

/^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$/

Matches the 3-part JWT format (header.payload.signature), base64-url encoded.

  • βœ… eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0…
  • ❌ abc.def
  • ❌ ey... (missing part)

This doesn't validate the signature or payload content.

πŸ‡«πŸ‡· France-Specific

Match SIREN (9 digits)

/^\d{9}$/
  • βœ… 732829320
  • ❌ 732 829 320
  • ❌ 1234567890

Match SIRET (14 digits)

/^\d{14}$/
  • βœ… 73282932000074
  • ❌ 732 829 320 00074

Match TVA intracommunautaire (French VAT)

/^FR[\dA-Z]{2}\d{9}$/
  • βœ… FR40303265045
  • βœ… FRXX732829320
  • ❌ FR40303265045123
  • ❌ FR40 3032 6504 5

Check rules per country for prefix and control key logic.

🀝 Contributing

You're welcome to contribute new patterns to this cheat sheet, or improve existing, either by the use of PR or issues!

To keep things accessible and consistent, please follow these guidelines:

  • Use the existing format for each entry:
    • A clear title (### level)
    • A brief, plain-language explanation of the use case
    • The regular expression inside a code block
    • βœ… Working examples and ❌ non-matching examples
    • A short tip to adapt or tweak the expression (when applicable)

Example:

### Match UPPERCASE words
/^[A-Z]+$/
Matches strings with only uppercase letters.

- βœ… HELLO
- βœ… REGEXP
- ❌ Hello
- ❌ hello123

To allow numbers too, change to `/^[A-Z0-9]+$/`

---
  • Keep it simple and free of regex jargon – this is meant to be readable by everyone
  • Make sure every pattern is tested and working before submitting
  • Pull requests are reviewed and tested before merging – no untested expressions please πŸ™

πŸ“ License

This project is licensed under the MIT License. Feel free to use, copy, modify, and distribute – contributions welcome!

About

🧠 A practical, language-agnostic, copy-paste-ready cheat sheet of real-world RegExp patterns, organized by category. From emails and dates to URLs, binary formats, colors, and beyond β€” no theory, just patterns that work.

Topics

Resources

License

Stars

Watchers

Forks