Regex Tester & Builder

Pattern matching & validation tool • 2026 dev

Regular Expression Syntax:

Show the builder

Pattern: /pattern/flags

Common Metacharacters:

  • . - Any character except newline
  • ^ - Start of string
  • $ - End of string
  • * - Zero or more
  • + - One or more
  • ? - Zero or one
  • [] - Character class
  • () - Grouping
  • | - Alternation (OR)

Character Classes:

  • \d - Digit [0-9]
  • \w - Word character [a-zA-Z0-9_]
  • \s - Whitespace
  • \D - Non-digit
  • \W - Non-word character
  • \S - Non-whitespace

Example: Email validation pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/

This pattern matches email addresses by ensuring they have a username part, @ symbol, domain, and top-level domain.

Regex Pattern

Advanced Options

Regex Results

Matches Found
0
Valid Pattern
Yes
Execution Time
0ms
Groups Captured
0

Global

Enabled

Case Insensitive

Enabled

Pattern

[a-z]+@[a-z]+\.[a-z]{2,}

Interpretation
Enter a regex pattern and test string to see results interpretation.

Match Results

Enter a pattern and test string to see matches highlighted here.

Comprehensive Regex Guide

What is Regular Expression?

Regular expressions (regex) are sequences of characters that define search patterns. They are used for string matching, validation, searching, and text manipulation. Regex is a powerful tool for developers to process and validate text efficiently.

Common Patterns

Essential Regex Patterns:

  • ^ - Start of string anchor
  • $ - End of string anchor
  • . - Any character except newline
  • \d - Digit [0-9]
  • \w - Word character [a-zA-Z0-9_]
  • \s - Whitespace character
  • [abc] - Character class (a, b, or c)
  • [a-z] - Range (any lowercase letter)
  • + - One or more
  • * - Zero or more
  • ? - Zero or one (optional)
  • {n,m} - Between n and m times
  • (...) - Grouping/capturing
  • | - Alternation (OR operator)
Practical Examples
1
Email Validation: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
2
Phone Number: /^\+?[\d\s\-\(\)]{10,}$/
3
URL Validation: /^https?:\/\/[^\s/$.?#].[^\s]*$/
4
IP Address: /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
Regex Flags

Common flags that modify regex behavior:

  • g (Global): Find all matches, not just the first
  • i (Ignore Case): Case-insensitive matching
  • m (Multiline): ^ and $ match line boundaries
  • s (Dotall): . matches newline characters
  • u (Unicode): Enable Unicode support
  • y (Sticky): Perform sticky matching
Best Practices
  • Be Specific: Use character classes and quantifiers carefully
  • Test Thoroughly: Validate with various inputs
  • Escape Special Characters: Use \ to escape metacharacters
  • Optimize Performance: Avoid catastrophic backtracking
  • Use Tools: Leverage online regex testers and builders

Regex Fundamentals

What is Regex?

Pattern matching for text processing.

Basic Syntax

/pattern/flags

Defines search patterns in strings.

Key Rules:
  • Escape special characters
  • Use anchors for boundaries
  • Test with diverse inputs

Regex Applications

When to Use Regex

For pattern matching and text validation.

Common Uses
  1. Input validation
  2. Data extraction
  3. Text replacement
  4. Format checking
Considerations:
  • Performance with complex patterns
  • Readability of expressions
  • Language-specific variations
  • Security implications

Regex Learning Quiz

Question 1: Multiple Choice - Understanding Regex Components

What does the regex pattern /[a-z]+/ match?

Solution:

The answer is B) One or more consecutive lowercase letters. The pattern [a-z] defines a character class that matches any lowercase letter from 'a' to 'z'. The + quantifier means "one or more" of the preceding element, so [a-z]+ matches one or more consecutive lowercase letters.

Pedagogical Explanation:

Breaking down the pattern: [a-z] is a character class that matches any single character in the range from 'a' to 'z'. The + is a quantifier that specifies that the preceding element (the character class) must occur one or more times. So "hello" would match, but "hi there" would match only "hi" and "there" separately.

Key Definitions:

Character Class: [...] matches any single character inside brackets

Quantifier: Specifies how many times a character or group repeats

Plus (+): One or more occurrences

Important Rules:

• + means one or more

• * means zero or more

• ? means zero or one

Tips & Tricks:

• Use {n,m} for exact range

• [A-Za-z] for all letters

• \w matches word characters

Common Mistakes:

• Confusing + with *

• Forgetting that + requires at least one match

• Misunderstanding character class ranges

Question 2: Regex Pattern Application

Which of the following strings would match the regex pattern /^\d{3}-\d{2}-\d{4}$/? (This pattern is commonly used for US Social Security Numbers)

Solution:

Let's break down the pattern /^\d{3}-\d{2}-\d{4}$/:

  • ^ - Start of string
  • \d{3} - Exactly 3 digits
  • - - Literal hyphen character
  • \d{2} - Exactly 2 digits
  • - - Literal hyphen character
  • \d{4} - Exactly 4 digits
  • $ - End of string

So the pattern matches strings in the format "XXX-XX-XXXX" where each X is a digit. For example: "123-45-6789", "987-65-4321", "001-23-4567".

Pedagogical Explanation:

This pattern demonstrates several important regex concepts: anchors (^ and $) ensure the entire string matches the pattern, not just a substring. The \d shorthand matches any digit (equivalent to [0-9]). The {n} quantifier specifies exactly n occurrences. The literal hyphens are not escaped because they're not inside a character class where they might have special meaning.

Key Definitions:

Anchors: ^ start, $ end of string

Shorthand Classes: \d digits, \w word chars

Exact Quantifier: {n} exactly n times

Important Rules:

• Use anchors to match entire string

• Shorthand classes are convenient

• Quantifiers specify repetition counts

Tips & Tricks:

• {n,m} for minimum to maximum

• {n,} for at least n times

• Escape special chars when literal needed

Common Mistakes:

• Forgetting anchors allows partial matches

• Confusing \d with \D

• Misplacing quantifiers

Question 3: Word Problem - Email Validation

Create a regex pattern to validate basic email addresses. The pattern should match strings with a username part (letters, numbers, dots, underscores), followed by '@', then a domain name (letters, numbers, dots), and finally a top-level domain (at least 2 letters).

Solution:

A suitable pattern would be: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/

Breaking it down:

  • ^ - Start of string
  • [a-zA-Z0-9._%+-]+ - Username: one or more allowed characters
  • @ - Literal @ symbol
  • [a-zA-Z0-9.-]+ - Domain: one or more allowed characters
  • \. - Literal dot (escaped)
  • [a-zA-Z]{2,} - Top-level domain: 2 or more letters
  • $ - End of string

This pattern would match: "user@example.com", "john.doe@company.org", "test_email@sub.domain.co.uk"

Pedagogical Explanation:

This pattern demonstrates advanced regex concepts. The username part allows common characters found in email usernames. The domain part is more restrictive. Note that the dot in the TLD part must be escaped (\.) because it's outside of a character class where it would otherwise match any character. The {2,} quantifier ensures at least 2 letters in the TLD.

Key Definitions:

Escaping: Using \ to treat special chars literally

Character Class: Set of allowed characters

Minimum Quantifier: {n,} at least n times

Important Rules:

• Always escape special chars when literal needed

• Use character classes for allowed sets

• Anchors prevent partial matches

Tips & Tricks:

• Test with valid and invalid examples

• Start simple, add complexity gradually

• Use online testers to validate

Common Mistakes:

• Forgetting to escape the dot in TLD

• Not anchoring the pattern

• Being too restrictive or too permissive

Question 4: Application-Based Problem - Phone Number Validation

Create a regex pattern to validate US phone numbers in the format (XXX) XXX-XXXX or XXX-XXX-XXXX. The pattern should be flexible enough to handle optional country codes and spaces.

Solution:

A flexible pattern would be: /^(\+\d{1,3}[-.\s]?)?\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})$/

Breaking it down:

  • ^ - Start of string
  • (\+\d{1,3}[-.\s]?)? - Optional country code
  • \(?(\d{3})\)? - Area code (with optional parentheses)
  • [-.\s]? - Optional separator
  • (\d{3}) - Next 3 digits
  • [-.\s]? - Optional separator
  • (\d{4}) - Last 4 digits
  • $ - End of string

This pattern would match: "(123) 456-7890", "123-456-7890", "+1 123 456 7890", "123.456.7890"

Pedagogical Explanation:

This pattern shows advanced concepts like optional groups and alternation. The (...)? syntax makes the entire group optional. Parentheses create capturing groups that can be referenced later. The flexibility comes from allowing optional separators and formatting variations while maintaining the core digit pattern.

Key Definitions:

Capturing Group: (...) creates a group that remembers matched text

Optional Group: (...)? matches zero or one time

Alternation: | operator for OR logic

Important Rules:

• Group related elements together

• Use optional quantifiers for flexibility

• Balance specificity with usability

Tips & Tricks:

• Use non-capturing groups (?:...) when not needed

• Test with various formats

• Consider international formats

Common Mistakes:

• Creating overly complex patterns

• Not considering all valid formats

• Forgetting to escape special characters

Question 5: Multiple Choice - Regex Flags

Which regex flag would you use to make a pattern case-insensitive?

Solution:

The answer is B) i (ignore case). The 'i' flag makes the regex pattern case-insensitive, so it treats uppercase and lowercase letters as equivalent. For example, the pattern /hello/i would match "Hello", "HELLO", "hello", "HeLLo", etc.

Pedagogical Explanation:

Regex flags modify how the pattern matching behaves. The 'i' flag is particularly useful when dealing with user input where case variations are expected. Other flags include 'g' for global matching (find all occurrences), 'm' for multiline mode (where ^ and $ match line boundaries), and 's' for dotall mode (where . matches newlines).

Key Definitions:

Flags: Modifiers that change regex behavior

Case-insensitive: Ignores letter case during matching

Global: Matches all occurrences, not just first

Important Rules:

• 'i' flag enables case-insensitive matching

• Flags are applied after the closing slash

• Multiple flags can be combined

Tips & Tricks:

• Combine flags: /pattern/gim

• Use 'g' for replacing all matches

• 'm' is useful for line-by-line processing

Common Mistakes:

• Confusing flag purposes

• Forgetting to add flags when needed

• Not understanding flag interactions

Regex Tester & Builder

FAQ

Q: What's the difference between . and \. in regex?

A: The dot (.) is a metacharacter that matches any character except newline, while \. matches a literal dot character. The backslash escapes the special meaning of the dot, treating it as a regular character. For example:

/file./ matches "file1", "fileA", "file!", etc.

/file\./ matches "file.txt", "file.pdf", "file.doc", etc.

This escaping principle applies to all metacharacters: *.+?^$[]{|()} need to be escaped with \ to match literally.

Q: How can I validate that a string contains only alphanumeric characters?

A: Use the pattern /^[a-zA-Z0-9]+$/ to match strings containing only alphanumeric characters:

^ - Start of string

[a-zA-Z0-9] - Character class matching letters and numbers

+ - One or more of the preceding character class

$ - End of string

Alternatively, you can use /^\w+$/ with the restriction that \w also includes underscore. For strict alphanumeric without underscore: /^[a-zA-Z0-9]+$/

About

Developer Team
This calculator was created
This calculator was created by our Developer Tools Team , may make errors. Consider checking important information. Updated: April 2026.