Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Regex Laravel Package

php-standard-library/regex

Type-safe regex for PHP with typed capture groups and predictable error handling. Build expressions with confidence, get structured match results, and avoid silent failures common in preg_* functions. Part of PHP Standard Library.

View on GitHub
Deep Wiki
Context7

Type-safe regular expressions with typed capture groups and proper error handling

Frequently asked questions about Regex
Can I use this package to replace preg_match in Laravel validation rules?
Yes, this package is designed to work seamlessly with Laravel’s custom validation rules. Replace `preg_match` calls with the fluent API (e.g., `Regex::match()->pattern('/.../')->capture('group')`) to enforce type safety and structured results. The structured `MatchResult` objects integrate cleanly with Laravel’s validation error handling when combined with custom `Rule` classes.
Does this work with Laravel’s built-in Validator or only custom rules?
While it integrates directly with custom `Rule` classes, the package doesn’t natively extend Laravel’s `Validator` facade. You’ll need to wrap results in a `ValidationException` manually or create a custom validator extension. For most use cases, custom rules (e.g., `extends Rule`) are the recommended approach to leverage its type safety.
What Laravel versions does this package support?
The package targets PHP 8.1+, which aligns with Laravel 9+ and 10. For older Laravel versions (8.x), you may encounter compatibility issues due to PHP 8.0’s lack of attributes or union types. Always check the [PHP Standard Library docs](https://php-standard-library.dev) for version-specific notes before adopting.
How do I handle errors if a regex pattern is invalid?
Unlike `preg_last_error()`, this package throws exceptions (e.g., `InvalidPatternException`) for malformed regex. Catch these exceptions and convert them to Laravel’s `ValidationException` or log them via `Log::error()`. The structured errors make debugging easier in Laravel’s monolithic request lifecycle compared to `preg_*` ambiguity.
Will this slow down my Laravel application?
The package introduces minimal overhead for most use cases, but complex regex (e.g., recursive patterns) may still hit PHP’s PCRE limits. Benchmark critical paths (e.g., API validation) against raw `preg_*` using tools like PHPBench. For trivial cases, the `Regex::raw()` fallback ensures no performance regression.
Can I use typed capture groups in Blade templates?
Not directly—Blade templates aren’t designed for complex logic like typed regex. Use this package in service layers (e.g., `FormRequest` validation) or controllers, then pass structured results to Blade. For simple cases, stick with `preg_match` in Blade or refactor logic to a helper class using this library.
How do I migrate existing preg_match calls to this package?
Start with a pilot module (e.g., user input validation) to validate the wrapper’s effectiveness. Use search/replace for simple cases (e.g., `preg_match('/.../', $str)` → `Regex::match()->pattern('/.../')->test($str)`), then update tests to handle structured `MatchResult` objects. For legacy codebases, consider a phased migration.
Does this package support async/queue jobs in Laravel?
Yes, it’s ideal for background jobs like CSV/JSON parsing or report generation. The structured return types simplify unit testing for queued logic, and the fluent API reduces boilerplate in job handlers. Just ensure your job’s dependencies are properly injected (e.g., via constructor or `handle()` method).
Are there alternatives for type-safe regex in Laravel?
For Laravel-specific needs, consider `spatie/laravel-regex` (simpler but less type-safe) or `symfony/validator` (for constraint-based validation). This package stands out by combining PHP’s type system with regex, but it lacks Laravel-native features like Blade directives. Choose based on whether you prioritize type safety or ecosystem integration.
How do I test regex logic using this package?
The structured `MatchResult` objects simplify unit testing. Assert against properties like `MatchResult::hasMatch()` or `MatchResult::getCapture('group')` in PHPUnit. For validation rules, test both success and failure cases by catching exceptions. The package’s design reduces flakiness from `preg_*`’s silent failures.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor