Regex Cheatsheet

Developer Playground

Common regex patterns for different programming languages

Language-specific Regex Cheatsheet

JavaScript/TypeScript

  • Email:

    ^[\w.-]+@[\w.-]+\.[\w.-]+$
  • URL:

    ^https?://[\w.-]+\.[\w.-]+(/[\w.-]*)*$
  • Phone Number:

    ^\d{2,3}-\d{3,4}-\d4$
  • IPv4 Address:

    ^(\d{1,3}\.)3\d{1,3}$
  • IPv6 Address:

    ^([0-9a-fA-F]{1,4}:)7[0-9a-fA-F]{1,4}$
  • Date (YYYY-MM-DD):

    ^\d4-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
  • Credit Card:

    ^\d4[\s-]?\d4[\s-]?\d4[\s-]?\d4$
  • ZIP Code (US):

    ^\d5(-\d4)?$
  • Hexadecimal Color:

    ^#([A-Fa-f0-9]6|[A-Fa-f0-9]3)$

Python

  • Email:

    r'^[\w.-]+@[\w.-]+\.[\w.-]+$'
  • URL:

    r'^https?://[\w.-]+\.[\w.-]+(/[\w.-]*)*$'
  • Phone Number:

    r'^\d{2,3}-\d{3,4}-\d4$'
  • IPv4 Address:

    r'^(\d{1,3}\.)3\d{1,3}$'
  • IPv6 Address:

    r'^([0-9a-fA-F]{1,4}:)7[0-9a-fA-F]{1,4}$'
  • Date (YYYY-MM-DD):

    r'^\d4-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$'
  • Credit Card:

    r'^\d4[\s-]?\d4[\s-]?\d4[\s-]?\d4$'
  • ZIP Code (US):

    r'^\d5(-\d4)?$'
  • Hexadecimal Color:

    r'^#([A-Fa-f0-9]6|[A-Fa-f0-9]3)$'

Java/Kotlin

  • Email:

    ^[\\w.-]+@[\\w.-]+\\.[\\w.-]+$
  • URL:

    ^https?://[\\w.-]+\\.[\\w.-]+(/[\\w.-]*)*$
  • Phone Number:

    ^\\d{2,3}-\\d{3,4}-\\d4$
  • IPv4 Address:

    ^(\\d{1,3}\\.)3\\d{1,3}$
  • IPv6 Address:

    ^([0-9a-fA-F]{1,4}:)7[0-9a-fA-F]{1,4}$
  • Date (YYYY-MM-DD):

    ^\\d4-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01])$
  • Credit Card:

    ^\\d4[\\s-]?\\d4[\\s-]?\\d4[\\s-]?\\d4$
  • ZIP Code (US):

    ^\\d5(-\\d4)?$
  • Hexadecimal Color:

    ^#([A-Fa-f0-9]6|[A-Fa-f0-9]3)$

Go

  • Email:

    `^[\w.-]+@[\w.-]+\.[\w.-]+$`
  • URL:

    `^https?://[\w.-]+\.[\w.-]+(/[\w.-]*)*$`
  • Phone Number:

    `^\d{2,3}-\d{3,4}-\d4$`
  • IPv4 Address:

    `^(\d{1,3}\.)3\d{1,3}$`
  • IPv6 Address:

    `^([0-9a-fA-F]{1,4}:)7[0-9a-fA-F]{1,4}$`
  • Date (YYYY-MM-DD):

    `^\d4-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$`
  • Credit Card:

    ^\d4[\s-]?\d4[\s-]?\d4[\s-]?\d4$
  • ZIP Code (US):

    ^\d5(-\d4)?$
  • Hexadecimal Color:

    ^#([A-Fa-f0-9]6|[A-Fa-f0-9]3)$

Rust

  • Email:

    r"^[\w.-]+@[\w.-]+\.[\w.-]+$"
  • URL:

    r"^https?://[\w.-]+\.[\w.-]+(/[\w.-]*)*$"
  • Phone Number:

    r"^\d{2,3}-\d{3,4}-\d4$"
  • IPv4 Address:

    r"^(\d{1,3}\.)3\d{1,3}$"
  • IPv6 Address:

    r"^([0-9a-fA-F]{1,4}:)7[0-9a-fA-F]{1,4}$"
  • Date (YYYY-MM-DD):

    r"^\d4-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$"
  • Credit Card:

    r"^\d4[\s-]?\d4[\s-]?\d4[\s-]?\d4$"
  • ZIP Code (US):

    r"^\d5(-\d4)?$"
  • Hexadecimal Color:

    r"^#([A-Fa-f0-9]6|[A-Fa-f0-9]3)$"

Regular Expression Guide

What is Regular Expression?

Regular expressions (regex) are powerful tools for finding or matching specific patterns in strings. They are used for text search, validation, string replacement, and many other purposes.

Basic Metacharacters

.Any single character
*Zero or more times
+One or more times
?Zero or one time
^Start of string
$End of string

Character Classes

\dDigits [0-9]
\wWord chars [a-zA-Z0-9_]
\sWhitespace
[a-z]Lowercase a-z
[A-Z]Uppercase A-Z
[^abc]Not a, b, or c

Quantifiers

{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times
*?Non-greedy zero or more
+?Non-greedy one or more

Groups and Capture

()Capture group
(?:)Non-capture group
|OR operator
\1First group reference

Practical Examples

Email validation:

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

Phone number (Korea):

^01[016789]-\d{3,4}-\d{4}$

URL validation:

^https?://[\w.-]+\.[a-zA-Z]{2,}.*$

Extract numbers only:

\d+

💡 Tips and Best Practices

  • • Each language has different backslash (\) escaping methods
  • • Use raw strings to avoid backslash issues
  • • Add comments for complex regex patterns to improve readability
  • • String methods might be faster than regex for performance-critical cases
  • • Use online regex testers to validate your patterns

Advertisement