Skip to content

Regular Expressions Cheat Sheet

The Power of Regular Expressions (RegEx)

homepage-banner

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")
else:
    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

/([a-z0-9_\-.+]+)@\w+(\.\w+)*

IP address

\b(?:\d{1,3}\.){3}\d{1,3}\b

Credentials

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)

Address

(street|st|road|rd|avenue|ave|drive|dr|loop|court|ct|circle|cir|lane|ln|boulevard|blvd|way)\.?\b

Social security number

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

ZIP code

\b\d{5}\b(-\d{4})?\b

URL

([^\s:/?#]+):\/\/([^/?#\s]*)([^?#\s]*)(\?([^#\s]*))?(#([^\s]*))?

Dates

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

MD5 hash

^[a-f0-9]{32}$

SHA1 hash

\b([a-f0-9]{40})\b

Base64 encoding

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

SQL statements

(LECT\s[\w\*\)\(\,\s]+\sFROM\s[\w]+)|(UPDATE\s[\w]+\sSET\s[\w\,\'\=]+)|(INSERT\sINTO\s[\d\w]+[\s\w\d\)\(\,]*\sVALUES\s\([\d\w\'\,\)]+)

Private IP

(^127\.)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)

Conclusion

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.

Reference

  • regexr.com
  • regex101.com
  • Practical Security Automation and Testing (Tony Hsiang-Chih Hsu)
Leave a message