DEV Community

Cover image for Regex Cheat Sheet - Regular Expressions Tutorial
EmreKanbay
EmreKanbay

Posted on • Originally published at kanby.net

Regex Cheat Sheet - Regular Expressions Tutorial

This blog post is firstly published on my personal blogging site kanby.net

TL;DR

  • Most common regex patterns
  • Examples of regex patterns

Most Common Patterns

Important: a x b in this example a is preceeding by x and b is followed by x

  • ^ means start of each line in string
  • $ means end of each line in string
  • () groups spesific pattern
  • (?:) means this pattern will match in string BUT will not return it. Eg. A phone number regex pattern must include country code but does not have to extract that part even though it must match on string.
  • [] matches specified single character, eg. [abc] will macth a or b or c. Ranges are supported too [a-c] will match same.
  • [^] matches every character except specified ones
  • . matches any single character
  • * 0 or more of the preceding element
  • + 1 or more of the preceding element
  • ? 0 or 1 of the preceding element
  • {n} Exactly n occurrences of the preceding element.
  • {n,} n or more occurrences of the preceding element.
  • {n,m} Between n and m occurrences of the preceding element
  • \d any digit
  • \D any non-digit
  • \w any word character (alphanumeric + underscore)
  • \W any non-word character
  • \s Matches any whitespace character
  • \S Matches any non-whitespace character
  • \ escape character, eg. İf you want to find . (which is a special character) in your string, you need to do this \.

With combining these, you can create highly complicated pattern match/extraction functions with Regex.

Examples

  1. \^[a-z_]+\.com$\ will match .com domains

    • [a-z_] means characters from a to z and underscore
    • + means at least one of them
    • \. means period (.)
    • com is for just com
    • ^ and $ is for searching from start of strin to end of each line
  2. ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$ will match emails

  3. ^ Asserts the start of the string.

  4. [a-zA-Z0-9._%+-]+ Matches one or more characters that can be:

    • Letters (a-z, A-Z)
    • Digits (0-9)
    • Underscores (_)
    • Dots (.)
    • Percent signs (%)
    • Plus signs (+)
    • Hyphens (-)
  • @ Matches the "@" symbol, which is mandatory in all email addresses.

  • [a-zA-Z0-9.-]+ Matches one or more characters for the domain name, allowing:

    • Letters (a-z, A-Z)
    • Digits (0-9)
    • Hyphens (-)
    • Dots (.)
  • . Matches a literal dot (.) separating the domain name from the top-level domain (TLD).

  • [a-zA-Z]{2,} Matches the top-level domain (TLD) consisting of at least two letters (e.g., .com, .org).

  • $ Asserts the end of the string.

If you have any questions, Here is my Instagram @emrekanbay.en

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free