Skip to content

Regular Expressions

Regular Expressions Cheat Sheet

As programmers, we often come across situations where we need to manipulate strings. This could be anything from validating user input to searching for specific patterns within a larger text. This is where Regular Expressions or RegEx comes in.

RegEx is a powerful tool used to match and manipulate text based on patterns. It is supported by most modern programming languages and is used extensively in web development, data analysis, and text processing.

Anatomy of a RegEx

A RegEx consists of a sequence of characters that define a search pattern. These characters can be used to match specific parts of a string, such as digits, letters, or special characters. Here are some examples of RegEx patterns:

  • [0-9] : Matches any digit from 0 to 9
  • [a-z] : Matches any lowercase letter from a to z
  • . : Matches any character except for a newline
  • \s : Matches any whitespace character
  • \d : Matches any digit

We can also use special characters like ^ to match the beginning of a string and $ to match the end of a string. For example, the pattern ^hello would match any string that starts with the word “hello”. Similarly, the pattern world$ would match any string that ends with the word “world”.

Using RegEx in Programming

RegEx can be used in most programming languages through libraries or built-in functions. For example, in Python, we can use the re library to work with RegEx. Here is an example of using RegEx to match a phone number:

import re

phone_number = "123-456-7890"

# match the phone number using RegEx
if re.match(r'\d{3}-\d{3}-\d{4}', phone_number):
    print("Valid phone number")
    print("Invalid phone number")

Examples of regular expression

Credit card number

\d{4}[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}|\d{4}[ -]?\d{6}[ - ]?\d{4}\d?

Email address


IP address



1234 | admin | password | pass | creds | login

Phone number

(\(?\+?[0-9]{1,2}\)?[-. ]?)?(\(?[0-9]{3}\)?|[0-9]{3})[-. ]?([0-9]{3}[-. ]?[0-9]{4}|\b[A-Z0-9]{7}\b)



Social security number

\b\d{3}[ -.]?\d{2}[ -.]?\d{4}\b

ZIP code






MD5 hash


SHA1 hash


Base64 encoding


SQL statements


Private IP



In conclusion, Regular Expressions are an incredibly powerful tool for manipulating text. Learning how to use RegEx can be a daunting task, but it is well worth the effort. With RegEx, we can easily validate user input, extract data, and search for specific patterns within text. By mastering RegEx, you can become a more efficient and effective programmer.


  • Practical Security Automation and Testing (Tony Hsiang-Chih Hsu)