Documentation/Regex Patterns

Regex Patterns

Master advanced URL matching with regular expressions to create powerful and flexible redirect rules.

!Quick Start

Enable regex mode when creating redirects to unlock pattern matching capabilities:

Source URL: /blog/(\d+)/([^/]+)
Target URL: /articles/$1-$2
Matches: /blog/123/hello-world → /articles/123-hello-world

Basic Concepts

What are Regex Patterns?

Regular expressions (regex) are powerful pattern-matching tools that allow you to match URLs based on patterns rather than exact strings. This enables dynamic redirects that can handle variable content like IDs, slugs, dates, and more.

When to Use Regex

✅ Good Use Cases

  • • Migrating blog posts with IDs
  • • Redirecting dated URLs
  • • Handling multiple similar patterns
  • • URL structure changes
  • • Category/tag reorganization

❌ Avoid When

  • • Simple exact URL matches
  • • Only a few static redirects
  • • Performance is critical
  • • Team lacks regex knowledge
  • • Pattern is overly complex

Common Patterns

Capture Numbers

(\d+)

Matches one or more digits

/product/123 → captures '123'

Capture Words/Slugs

([^/]+)

Matches any character except forward slash

/blog/my-awesome-post → captures 'my-awesome-post'

Optional Segments

/?([^/]*)

Makes a URL segment optional

/page or /page/extra → captures '' or 'extra'

Match Year/Date

(\d{4})/(\d{2})/(\d{2})

Matches YYYY/MM/DD format

/2023/12/25 → captures '2023', '12', '25'

File Extensions

([^/]+)\.(html?|php)$

Matches files with specific extensions

/page.html → captures 'page', 'html'

Wildcard Everything

(.*)

Captures everything (use carefully!)

/old/path/anything → captures 'old/path/anything'

Practical Examples

Blog Migration

Moving from WordPress to static site

/blog/(\d+)/([^/]+)/?
/articles/$2-$1
/blog/123/hello-world/articles/hello-world-123
/blog/456/react-tutorial//articles/react-tutorial-456

Date-based URLs

Reorganizing dated content

/(\d{4})/(\d{2})/(\d{2})/([^/]+)/?
/archive/$1/$4
/2023/12/25/christmas-post/archive/2023/christmas-post
/2024/01/01/new-year-goals//archive/2024/new-year-goals

Category Restructure

Flattening category structure

/category/([^/]+)/([^/]+)/?
/topics/$2
/category/tech/javascript/topics/javascript
/category/design/ui-patterns//topics/ui-patterns

File Extension Removal

Removing .html extensions

/([^/]+)\.html?$
/$1
/about.html/about
/contact.htm/contact

Subdomain to Path

Moving from subdomain to path-based structure

^/(.*)$
/blog/$1

💡 Use with domain-specific conditions

/post-title/blog/post-title
/about/blog/about

Advanced Features

Named Capture Groups

Use named groups for better readability and maintenance:

Source: /blog/(?<id>\d+)/(?<slug>[^/]+)
Target: /articles/${slug}-${id}

Lookaheads and Lookbehinds

Positive Lookahead

blog(?=/\d+)

Matches "blog" only if followed by "/digits"

Negative Lookbehind

(?<!old)/page

Matches "/page" not preceded by "old"

Conditional Logic

Use conditional patterns for complex routing logic:

/(mobile|m)\\.example\\.com/(.*)$
Target: /mobile/$2
Handles both mobile.example.com and m.example.com

Testing & Debugging

Built-in Pattern Tester

Use RedirectFlow's built-in regex tester to validate your patterns before deployment:

Pattern validates successfully
Syntax error detected

External Testing Tools

Recommended Tools

  • • regex101.com
  • • regexr.com
  • • regexpal.com

Testing Checklist

  • • Test expected matches
  • • Test edge cases
  • • Verify capture groups
  • • Check performance

Common Debugging Issues

Pattern not matching

Check for unescaped special characters (. + * ? ^ $ { } [ ] \ | ( ))

💡 Use \. instead of . to match literal dots

Too many matches

Make patterns more specific with anchors (^ for start, $ for end)

💡 ^/blog/(\d+)$ instead of /blog/(\d+)

Capture groups not working

Verify parentheses placement and $1, $2 references in target

💡 Check that capture group count matches usage

Performance & Best Practices

✅ Do's

Use specific patterns over broad ones
Anchor patterns with ^ and $ when possible
Test patterns thoroughly before deployment
Document complex regex patterns
Use non-capturing groups (?:) when not needed
Limit backtracking with atomic groups
Consider order of regex rules

❌ Don'ts

Don't use .* without anchors
Avoid nested quantifiers like (a+)+
Don't create overly complex patterns
Avoid excessive capture groups
Don't ignore case sensitivity issues
Don't forget to escape special characters
Avoid patterns that can cause infinite loops

Quick Reference

.Any character
*Zero or more
+One or more
?Zero or one
^Start of string
$End of string
\dAny digit
\wAny word character
\sAny whitespace
[abc]Any of a, b, or c
[^abc]Not a, b, or c
()Capture group
(?:)Non-capture group
a|ba or b
{3}Exactly 3
{3,5}3 to 5

Ready to Get Started?

Start using regex patterns in your redirects to create powerful, dynamic routing rules.