# Error: Invalid middleware matcher. The pattern 'X' is not a valid regex or path pattern.

- **ID:** `nextjs/invalid-middleware-matcher-regex`
- **Domain:** nextjs
- **Category:** config_error
- **Verification:** ai_generated
- **Fix Rate:** 95%

## Root Cause

The matcher array in middleware.config contains a malformed regex or path pattern that Next.js cannot parse.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| next@13.4.0 | active | — | — |
| next@14.0.0 | active | — | — |
| next@14.2.0 | active | — | — |

## Workarounds

1. **Use the correct glob syntax for path matching. For example, to match all API routes:

export const config = {
  matcher: '/api/:path*',
};

For multiple patterns, use an array:
export const config = {
  matcher: ['/api/:path*', '/admin/:path*'],
};** (95% success)
   ```
   Use the correct glob syntax for path matching. For example, to match all API routes:

export const config = {
  matcher: '/api/:path*',
};

For multiple patterns, use an array:
export const config = {
  matcher: ['/api/:path*', '/admin/:path*'],
};
   ```
2. **If you need complex matching, use a custom middleware function that checks the request URL manually instead of relying on matcher.** (85% success)
   ```
   If you need complex matching, use a custom middleware function that checks the request URL manually instead of relying on matcher.
   ```
3. **Validate your pattern using the Next.js documentation: patterns use a simplified glob where '*' matches any path segment and ':param' captures named parameters.** (90% success)
   ```
   Validate your pattern using the Next.js documentation: patterns use a simplified glob where '*' matches any path segment and ':param' captures named parameters.
   ```

## Dead Ends

- **** — Next.js matcher patterns use a simplified glob syntax, not full regex. Forward slashes are reserved for path segments. (80% fail)
- **** — Characters like '.' or '*' need escaping in regex but Next.js matcher uses glob where '*' is a wildcard, causing ambiguity. (70% fail)
- **** — An empty array matches no routes, effectively disabling the middleware, which may not be the intended behavior. (50% fail)
