Regex Generator

Free online regex generator and tester with visual pattern builder. Create, test, and debug regular expressions easily. Includes pattern library, component builder, syntax highlighting, and code generation for multiple languages. Perfect for developers and data validation.

Library Pattern Library
Builder Pattern Builder
Enter your regex pattern or select from library
/pattern/flags
Components Quick Components
Click to insert component at cursor position
Test Test Input

Regex Generator - Visual Regular Expression Builder and Tester

A powerful online regex generator and tester with visual pattern builder. Create, test, and debug regular expressions with instant feedback. Includes comprehensive pattern library, quick component insertion, syntax highlighting, match visualization, and code generation for JavaScript, Python, PHP, Java, Ruby, and Go.

What are Regular Expressions?

**Regular Expressions (Regex)** are powerful pattern-matching tools used to search, validate, and manipulate text.

**Common Uses:**

1. **Validation:**
• Email addresses
• Phone numbers
• Passwords
• URLs
• Credit cards

2. **Text Processing:**
• Search and replace
• Data extraction
• Log parsing
• Text cleaning

3. **Data Validation:**
• Form inputs
• API parameters
• File formats
• Configuration files

**Basic Syntax:**

• **Literals:** Match exact characters
- `abc` matches "abc"

• **Character Classes:**
- `\d` = digit (0-9)
- `\w` = word character (a-z, A-Z, 0-9, _)
- `\s` = whitespace
- `.` = any character

• **Quantifiers:**
- `*` = 0 or more
- `+` = 1 or more
- `?` = 0 or 1 (optional)
- `{n}` = exactly n times
- `{n,m}` = between n and m times

• **Anchors:**
- `^` = start of line
- `$` = end of line
- `\b` = word boundary

• **Groups:**
- `(...)` = capturing group
- `(?:...)` = non-capturing group

• **Character Sets:**
- `[abc]` = matches a, b, or c
- `[a-z]` = matches any lowercase letter
- `[^abc]` = matches anything except a, b, or c

**Examples:**

```regex
# Email validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

# Phone number (US)
^\(?([0-9]{3})\)?[-.\s]?([0-9]{3})[-.\s]?([0-9]{4})$

# URL
^https?:\/\/[\w\-]+(\.[\w\-]+)+[/#?]?.*$

# Strong password
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
```

How to use this Regex Generator?

**Step-by-Step Guide:**

**1. Choose a Starting Point:**

**Option A - Pattern Library:**
• Browse common patterns (email, URL, phone, etc.)
• Click a pattern to load it
• Modify as needed

**Option B - Build from Scratch:**
• Type pattern directly in the input
• Use Quick Components to insert syntax
• Click components to add at cursor position

**2. Set Flags:**

• **g (Global):** Find all matches, not just first
• **i (Case Insensitive):** Ignore case (A = a)
• **m (Multiline):** ^ and $ match line breaks
• **s (Dot All):** . matches newlines

**3. Test Your Pattern:**

• Enter test text in the Test Input area
• Click "Test Pattern"
• View matches highlighted in real-time
• See match count and positions
• Inspect captured groups

**4. Understand Your Pattern:**

• Click "Explain Pattern"
• See breakdown of each component
• Learn what each symbol means
• Understand pattern structure

**5. Generate Code:**

• Click "Generate Code"
• Get ready-to-use code in:
- JavaScript
- Python
- PHP
- Java
- Ruby
- Go
• Copy to your project

**Tips:**

✓ Start with a pattern library example
✓ Test with multiple input variations
✓ Use Explain to learn syntax
✓ Test edge cases
✓ Keep patterns simple when possible
✓ Use non-capturing groups (?:) for performance
✓ Escape special characters: \. \* \+ \? etc.

Common Regex Patterns and Use Cases

**Validation Patterns:**

**1. Email Address:**
```regex
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
```
• Basic email validation
• Allows common special characters
• Requires @ and domain extension

**2. URL (HTTP/HTTPS):**
```regex
^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*)$
```
• Matches web URLs
• Optional www prefix
• Supports query strings and anchors

**3. Phone Numbers:**
```regex
# US Format
^\(?([0-9]{3})\)?[-.\s]?([0-9]{3})[-.\s]?([0-9]{4})$

# International
^\+?[1-9]\d{1,14}$
```

**4. Strong Password:**
```regex
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
```
• Minimum 8 characters
• At least one uppercase letter
• At least one lowercase letter
• At least one digit
• At least one special character

**Data Extraction:**

**5. Date Formats:**
```regex
# YYYY-MM-DD (ISO)
^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])$

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

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

**6. IP Addresses:**
```regex
# IPv4
^(?:(?: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]?)$

# IPv6 (simplified)
^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$
```

**7. Credit Card:**
```regex
# Visa, MasterCard, Amex, Discover
^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|6(?:011|5[0-9][0-9])[0-9]{12})$
```

**Text Processing:**

**8. Extract Hashtags:**
```regex
#[a-zA-Z0-9_]+
```

**9. Extract Mentions:**
```regex
@[a-zA-Z0-9_]+
```

**10. HTML Tags:**
```regex
<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)
```

**11. Remove Extra Whitespace:**
```regex
\s+
# Replace with single space
```

**Code Patterns:**

**12. JavaScript Variables:**
```regex
(var|let|const)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*=
```

**13. Hex Color Codes:**
```regex
^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$
```

**14. File Extensions:**
```regex
\.(jpg|jpeg|png|gif|pdf|doc|docx)$
```

**Tips:**

• Test patterns with edge cases
• Use online regex testers for debugging
• Consider performance for large texts
• Simpler is often better
• Document complex patterns

Regex Flags Explained

**Regular Expression Flags:**

Flags modify how the regex engine processes patterns.

**1. Global Flag (g):**

```javascript
const text = 'cat bat rat';
const regex1 = /at/; // No global
const regex2 = /at/g; // Global

text.match(regex1); // ['at'] - first match only
text.match(regex2); // ['at', 'at', 'at'] - all matches
```

**Use when:**
• Finding all occurrences
• Replace all instances
• Counting matches

**2. Case Insensitive Flag (i):**

```javascript
const regex1 = /hello/; // Case sensitive
const regex2 = /hello/i; // Case insensitive

regex1.test('Hello'); // false
regex2.test('Hello'); // true
regex2.test('HELLO'); // true
```

**Use when:**
• User input validation
• Case-insensitive search
• Flexible matching

**3. Multiline Flag (m):**

```javascript
const text = `line 1
line 2
line 3`;

const regex1 = /^line/; // Matches start of string
const regex2 = /^line/m; // Matches start of each line

text.match(regex1); // ['line'] - one match
text.match(regex2); // ['line', 'line', 'line'] - three matches
```

**Effect:**
• `^` matches start of each line (not just string)
• `$` matches end of each line (not just string)

**Use when:**
• Processing multi-line text
• Line-by-line validation
• Log file parsing

**4. Dot All Flag (s):**

```javascript
const text = 'hello\nworld';

const regex1 = /hello.world/; // . doesn't match \n
const regex2 = /hello.world/s; // . matches everything

regex1.test(text); // false
regex2.test(text); // true
```

**Effect:**
• `.` matches newline characters
• Normally `.` matches anything except `\n`

**Use when:**
• Matching across lines
• HTML/XML parsing
• Multi-line content extraction

**Flag Combinations:**

```javascript
// Multiple flags together
const regex = /pattern/gim;
// g = global
// i = case insensitive
// m = multiline

// Common combinations:
/email/gi // Find all emails, any case
/^error/gim // Find all lines starting with "error", any case
/.*?/gs // Match everything across lines, lazily
```

**Language-Specific Flags:**

**JavaScript:**
```javascript
/pattern/gimsuy
// y = sticky (matches from lastIndex)
// u = unicode (proper unicode handling)
```

**Python:**
```python
import re
re.IGNORECASE # i
re.MULTILINE # m
re.DOTALL # s
re.VERBOSE # x - ignore whitespace, allow comments
```

**PHP:**
```php
/pattern/gimsx
// x = extended (ignore whitespace)
```

**Best Practices:**

✓ Only use flags you need
✓ Global flag for replace/count operations
✓ Case insensitive for user input
✓ Multiline for text processing
✓ Test with and without flags
✓ Document flag usage in code comments

Advanced Regex Techniques

**1. Lookahead and Lookbehind:**

**Positive Lookahead (?=...)**
```regex
# Match 'foo' only if followed by 'bar'
foo(?=bar)

# Matches: 'foo' in 'foobar'
# Doesn't match: 'foo' in 'foobaz'
```

**Negative Lookahead (?!...)**
```regex
# Match 'foo' only if NOT followed by 'bar'
foo(?!bar)

# Matches: 'foo' in 'foobaz'
# Doesn't match: 'foo' in 'foobar'
```

**Positive Lookbehind (?<=...)**
```regex
# Match 'bar' only if preceded by 'foo'
(?<=foo)bar

# Matches: 'bar' in 'foobar'
# Doesn't match: 'bar' in 'bazbar'
```

**Negative Lookbehind (?<!...)**
```regex
# Match 'bar' only if NOT preceded by 'foo'
(?<!foo)bar

# Matches: 'bar' in 'bazbar'
# Doesn't match: 'bar' in 'foobar'
```

**2. Capturing Groups:**

```regex
# Extract parts of a date
(\d{4})-(\d{2})-(\d{2})

# Match: '2025-01-15'
# Group 1: '2025' (year)
# Group 2: '01' (month)
# Group 3: '15' (day)
```

**Named Capturing Groups:**
```regex
(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})

# Access by name in code:
# match.groups.year
# match.groups.month
# match.groups.day
```

**Non-Capturing Groups (?:...)**
```regex
# Group for alternation, but don't capture
(?:cat|dog)s?

# Matches: cat, cats, dog, dogs
# Doesn't create capture group
# Better performance
```

**3. Backreferences:**

```regex
# Match repeated words
\b(\w+)\s+\1\b

# Matches: 'the the', 'hello hello'
# \1 refers to first captured group
```

**HTML Tag Matching:**
```regex
# Match opening and closing tags
<([a-z]+)>.*?<\/\1>

# Matches: <div>content</div>
# Doesn't match: <div>content</span>
```

**4. Greedy vs Lazy Quantifiers:**

**Greedy (default):**
```regex
# Matches as much as possible
<.*>

# In: '<div>Hello</div> <span>World</span>'
# Matches: '<div>Hello</div> <span>World</span>' (entire string)
```

**Lazy (add ?):**
```regex
# Matches as little as possible
<.*?>

# In: '<div>Hello</div> <span>World</span>'
# Matches: '<div>', '</div>', '<span>', '</span>' (separately)
```

**Lazy quantifiers:**
• `*?` = 0 or more (lazy)
• `+?` = 1 or more (lazy)
• `??` = 0 or 1 (lazy)
• `{n,m}?` = n to m (lazy)

**5. Atomic Groups (?>...)**

```regex
# Prevent backtracking
(?>\d+)\.

# More efficient for long numbers
# Doesn't retry if decimal point doesn't match
```

**6. Conditional Patterns:**

```regex
# If group 1 matched, use pattern A, else pattern B
(\()?[^()]+(?(1)\))

# Matches: 'hello' or '(hello)'
# Doesn't match: 'hello)' or '(hello'
```

**7. Unicode Properties:**

```regex
# Match any letter (any language)
\p{L}+

# Match emoji
\p{Emoji}

# Match currency symbols
\p{Sc}
```

**Performance Tips:**

✓ Use non-capturing groups (?:) when don't need capture
✓ Be specific (avoid .* when possible)
✓ Use atomic groups to prevent backtracking
✓ Anchor patterns when possible (^, $, \b)
✓ Test with large inputs
✓ Avoid nested quantifiers
✓ Use character classes [abc] instead of (a|b|c)

**Common Pitfalls:**

❌ Catastrophic backtracking: `(a+)+b`
❌ Overly greedy: `.*` matching too much
❌ Missing anchors: partial matches
❌ Forgetting to escape: `. * + ? [ ] ( ) { } ^ $ | \`
❌ Not testing edge cases

Key Features

  • Visual pattern builder interface
  • Comprehensive pattern library
  • Common regex patterns (email, URL, phone, etc.)
  • Quick component insertion
  • Real-time pattern testing
  • Match highlighting and visualization
  • Captured groups display
  • Match count and positions
  • Pattern explanation generator
  • Component-by-component breakdown
  • Regex flags support (g, i, m, s)
  • Multi-language code generation
  • JavaScript code output
  • Python code output
  • PHP code output
  • Java code output
  • Ruby code output
  • Go code output
  • Syntax highlighting
  • Copy to clipboard
  • Download as code file
  • No data sent to server
  • Works offline
  • Mobile-friendly interface
  • Dark mode support
  • 100% free
  • No registration required