Tech News, Tech & Coding · Free Online Tools

IT Question Answer
Tools
Regex Cheat Sheet: 25 Essential Patterns for Data Validation and Extraction

Regex Cheat Sheet: 25 Essential Patterns for Data Validation and Extraction

Developer ToolsBy Muhammad Fareed10 min read
regexregular expressionsdata validationdata extractioncheat sheetprogrammingdeveloper tools

Understanding Regular Expressions (Regex)

Regular Expressions, commonly known as Regex, are powerful tools used for pattern matching within strings. They provide a concise and flexible way to identify specific sequences of characters, making them indispensable for tasks like data validation, extraction, search and replace operations, and text manipulation across various programming languages and tools.

This cheat sheet provides 25 practical regex patterns, focusing on common scenarios for data validation and extraction. Each pattern comes with a brief explanation and examples to help you quickly understand and implement them in your projects.

Regex Fundamentals: Key Concepts

Before diving into the patterns, a quick review of core regex components is helpful:

  • Anchors: ^ (start of string), $ (end of string).
  • Quantifiers: * (zero or more), + (one or more), ? (zero or one), {n} (exactly n times), {n,} (n or more times), {n,m} (between n and m times).
  • Character Classes: \d (any digit 0-9), \D (any non-digit), \w (any word character: a-z, A-Z, 0-9, _), \W (any non-word character), \s (any whitespace character: space, tab, newline), \S (any non-whitespace character), . (any character except newline).
  • Custom Character Sets: [abc] (matches a, b, or c), [^abc] (matches anything except a, b, or c), [a-z] (matches any lowercase letter).
  • Alternation: | (OR operator).
  • Grouping: () (groups expressions and creates capture groups).
  • Escaping: \ (used to escape special characters, e.g., \. to match a literal dot).
  • Lookaheads/Lookbehinds: Used for advanced assertions without consuming characters (e.g., (?=...) positive lookahead).

Essential Patterns for Data Validation

Data validation is crucial for maintaining data integrity. These patterns help ensure that user inputs or extracted data conform to expected formats.

Email Address Validation

Email addresses are complex, and a perfect regex is often debated. These patterns cover common scenarios.

1. Basic Email Validation

Matches a simple structure: something@something.domain. Not exhaustive but good for basic checks.

^\S+@\S+\.\S+$
  • ^: Start of the string.
  • \S+: One or more non-whitespace characters (for the username part).
  • @: Literal at symbol.
  • \S+: One or more non-whitespace characters (for the domain name).
  • \.: Literal dot.
  • \S+: One or more non-whitespace characters (for the top-level domain).
  • $: End of the string.

Examples:

  • valid@example.com (Match)
  • user123@sub.domain.co.uk (Match)
  • invalid email (No Match)

2. More Robust Email Validation

A more detailed pattern allowing letters, numbers, and specific symbols in username and domain, and a TLD of at least two letters.

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
  • ^[a-zA-Z0-9._%+-]+: Username: letters, numbers, ., _, %, +, -. One or more.
  • @: Literal at symbol.
  • [a-zA-Z0-9.-]+: Domain: letters, numbers, ., -. One or more.
  • \.: Literal dot.
  • [a-zA-Z]{2,}: TLD: at least two letters.

Examples:

  • test.user+tag@domain-name.co (Match)
  • another_email@sub.example.net (Match)
  • not@.com (No Match)

URL Validation

Ensuring web addresses are in a correct format.

3. Simple URL Validation (HTTP/HTTPS/FTP)

Checks for common protocols and a basic structure.

^(https?|ftp)://[^\\s/$.?#].[^\\s]*$
  • ^(https?|ftp)://: Starts with http, https, or ftp followed by ://.
  • [^\\s/$.?#]: Matches any character that is not whitespace, /, $, ., ?, #. This is a simplified approach to ensure it's not just a protocol.
  • .[^\\s]*$: Followed by any character (.) then zero or more non-whitespace characters ([^\\s]*) until the end of the string ($).

Examples:

  • http://www.example.com (Match)
  • ftp://ftp.test.org/file.zip (Match)
  • invalid url (No Match)

4. More Robust URL Validation

A more comprehensive pattern for URLs, allowing for subdomains, ports, paths, and query parameters.

^(https?|ftp)://([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(:[0-9]+)?(/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$

This pattern is significantly more complex, covering optional user info, various domain segments, ports, and common path characters. Due to its length, a detailed breakdown of each segment is extensive, but it generally accounts for protocol, optional authentication, domain, TLD, optional port, and optional path/query/fragment components.

Examples:

  • https://www.example.com/path/to/page?query=1&param=test#section (Match)
  • http://user:pass@sub.domain.co:8080/index.html (Match)
  • just-a-domain.com (No Match)

Phone Number Validation

Phone number formats vary greatly by region. These examples cover common US formats.

5. US Phone Number (XXX-XXX-XXXX)

Matches a strict 10-digit format with hyphens.

^\d{3}-\d{3}-\d{4}$
  • ^: Start of string.
  • \d{3}: Exactly three digits.
  • -: Literal hyphen.
  • \d{3}: Exactly three digits.
  • -: Literal hyphen.
  • \d{4}: Exactly four digits.
  • $: End of string.

Examples:

  • 123-456-7890 (Match)
  • (123) 456-7890 (No Match)

6. Flexible US Phone Number

Matches 10 digits, allowing for optional parentheses, spaces, or hyphens.

^\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})$
  • ^\(?: Optional opening parenthesis.
  • (\d{3}): Captures three digits.
  • \)?: Optional closing parenthesis.
  • [-.\s]?: Optional hyphen, dot, or space.
  • (\d{3}): Captures three digits.
  • [-.\s]?: Optional hyphen, dot, or space.
  • (\d{4})$: Captures four digits at the end of the string.

Examples:

  • (123) 456-7890 (Match)
  • 123.456.7890 (Match)
  • 123 456 7890 (Match)
  • 123-456-7890 (Match)
  • 1234567890 (Match)

Date Validation

Common date formats.

7. Date (YYYY-MM-DD)

Strictly matches four digits, two digits, two digits separated by hyphens.

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

Examples:

  • 2023-10-26 (Match)
  • 2023/10/26 (No Match)

8. Date (MM/DD/YYYY)

Strictly matches two digits, two digits, four digits separated by slashes.

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

Examples:

  • 10/26/2023 (Match)
  • 26-10-2023 (No Match)

Password Validation

Ensuring strong password policies.

9. Strong Password (Min 8 chars, 1 uppercase, 1 lowercase, 1 number, 1 special char)

Uses lookaheads for multiple conditions without consuming characters.

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[A-Za-z\d!@#$%^&*()_+]{8,}$
  • ^: Start of string.
  • (?=.*[a-z]): Positive lookahead for at least one lowercase letter.
  • (?=.*[A-Z]): Positive lookahead for at least one uppercase letter.
  • (?=.*\d): Positive lookahead for at least one digit.
  • (?=.*[!@#$%^&*()_+]): Positive lookahead for at least one specified special character.
  • [A-Za-z\d!@#$%^&*()_+]{8,}: Matches 8 or more characters from the allowed set (letters, digits, special characters).
  • $: End of string.

Examples:

  • Passw0rd! (Match)
  • MyStrongP@ss1 (Match)
  • weakpass (No Match - no uppercase, number, special)

Other Validation Patterns

10. IPv4 Address

Validates a standard IPv4 address format (e.g., 192.168.1.1).

^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$

This pattern ensures each segment is a number from 0-255.

Examples:

  • 192.168.1.1 (Match)
  • 10.0.0.255 (Match)
  • 256.0.0.1 (No Match)

11. US Zip Code (5 digits)

Matches exactly five digits.

^\d{5}$

Examples:

  • 12345 (Match)
  • 12345-6789 (No Match)

12. US Zip Code (5+4 digits)

Matches five digits, optionally followed by a hyphen and four more digits.

^\d{5}(-\d{4})?$

Examples:

  • 12345 (Match)
  • 12345-6789 (Match)
  • 1234 (No Match)

13. Alphanumeric String (no spaces, min 3, max 10 chars)

For usernames or IDs.

^[a-zA-Z0-9]{3,10}$

Examples:

  • User123 (Match)
  • ID_01 (No Match)

14. Username (alphanumeric, underscores, hyphens, 3-16 chars)

Common username format.

^[a-zA-Z0-9_-]{3,16}$

Examples:

  • my_user_name (Match)
  • another-user-1 (Match)
  • ab (No Match - too short)

15. Currency (e.g., $100.00 or 100.00)

Matches an optional dollar sign, then digits, optionally followed by two decimal places.

^\$?\d+(\.\d{2})?$
  • ^\$?: Optional dollar sign.
  • \d+: One or more digits.
  • (\.\d{2})?: Optional group for a dot followed by exactly two digits.

Examples:

  • $123.45 (Match)
  • 100.00 (Match)
  • 50 (Match)
  • $123.4 (No Match - not two decimal places)

Essential Patterns for Data Extraction

Regex excels at pulling out specific pieces of information from larger texts.

16. Extract All Numbers

Finds all sequences of one or more digits.

\d+

Example: In The year is 2023, and there are 365 days., extracts 2023, 365.

17. Extract Decimal Numbers

Finds numbers with a decimal point and at least one digit after it.

\d+\.\d+

Example: In Price: 19.99, Discount: 5.5%, Quantity: 10, extracts 19.99, 5.5.

18. Extract Words

Finds sequences of word characters (letters, numbers, underscore).

\b\w+\b
  • \b: Word boundary (ensures whole words are matched).
  • \w+: One or more word characters.

Example: In Hello, World! This is a test., extracts Hello, World, This, is, a, test.

19. Extract Hashtags

Identifies social media hashtags.

#\w+

Example: In Check out #devtools and #coding., extracts #devtools, #coding.

20. Extract Mentions

Identifies social media user mentions.

@\w+

Example: In Hey @john_doe, check this out @jane_smith., extracts @john_doe, @jane_smith.

21. Extract HTML/XML Tags

Captures any string that looks like an HTML or XML tag.

<[^>]+>
  • <: Literal opening angle bracket.
  • [^>]+: One or more characters that are NOT a closing angle bracket.
  • >: Literal closing angle bracket.

Example: In <div class="container"><p>Hello</p></div>, extracts <div class="container">, <p>, </p>, </div>.

22. Extract Text Inside Parentheses

Captures content enclosed in parentheses.

\(([^)]+)\)
  • \(: Literal opening parenthesis.
  • ([^)]+): Captures one or more characters that are NOT a closing parenthesis.
  • \): Literal closing parenthesis.

Example: In This is a test (with some extra info)., extracts with some extra info.

23. Extract Quoted Strings (single or double)

Finds text enclosed in either single or double quotes.

["']([^"']*)["']
  • ["']: Matches either a double quote or a single quote.
  • ([^"'']*): Captures zero or more characters that are NOT a double or single quote.
  • ["']: Matches the closing double or single quote.

Example: In She said "Hello World" and then 'Goodbye'., extracts Hello World, Goodbye.

24. Extract All Whitespace

Finds one or more contiguous whitespace characters.

\s+

Example: In This text has multiple spaces., extracts the various sequences of spaces.

25. Extract Specific Keyword (Case-Insensitive)

Finds a specific word regardless of its casing.

(?i)\b(keyword)\b
  • (?i): Flag for case-insensitive matching.
  • \b(keyword)\b: Matches the whole word "keyword".

Example: In Search for Keyword, KEYWORD, or keyword., extracts Keyword, KEYWORD, keyword.

Tips for Working with Regex

Regex can be tricky, but these tips can streamline your process:

  • Start Simple: Begin with the most basic pattern and gradually add complexity.
  • Test Iteratively: Use a regex tester to check your patterns against various inputs. This helps in debugging and refining your expressions quickly.
  • Understand Your Data: Know the exact format and variations of the data you need to match or extract.
  • Be Specific: Use anchors (^, $) and word boundaries (\b) to ensure you match exactly what you intend, avoiding partial or unintended matches.
  • Escape Special Characters: If you need to match a literal special character (like ., ?, *, +, (, ), [, ], {, }, |, ^, $, \), always escape it with a backslash (e.g., \. for a literal dot).
  • Use Non-Capturing Groups: If you group parts of your regex with () but don't need to extract their content, use non-capturing groups (?:...) for slight performance benefits and cleaner results if you're only interested in specific captured groups.

Key Takeaways and FAQs

What is Regex used for?

Regex is primarily used for pattern matching in text. This includes validating data formats (like email addresses or phone numbers), searching for specific patterns in large texts, extracting relevant information, and performing search-and-replace operations.

Why are some Regex patterns so long and complex?

The complexity of a regex pattern often correlates with the strictness and variety of rules it needs to enforce. For example, validating an email address requires accounting for many possible valid characters and structural variations, leading to longer patterns. Robust patterns aim to minimize false positives and false negatives.

Can Regex solve all text processing problems?

While extremely powerful, regex is not a silver bullet. For highly complex, nested, or recursive structures (like parsing arbitrary HTML or XML without specific structure knowledge), a full-fledged parser is generally more appropriate and reliable than regex alone. Regex is best suited for regular languages, which have patterns that can be defined without infinite recursion.

Where can I test these Regex patterns?

You can test these patterns, and build your own, using online tools like our Regex Tester. These tools allow you to input a pattern and sample text to see matches and captures in real-time.

Try Regex Tester & Generator free

Advertisement

Related posts