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.
Pros:
Illuminate\Validation ecosystem, enabling seamless adoption in custom validation rules (e.g., Rule objects) and form requests.preg_* calls with method chaining (e.g., Regex::match()->pattern('/.../')->capture('group')).preg_last_error()’s ambiguity, improving debugging in Laravel’s monolithic request lifecycle.MatchResult objects) simplify unit testing for regex-heavy logic, critical for Laravel’s TDD-friendly workflows.Cons:
preg_match calls in controllers).preg_match in custom Rule classes or FormRequest validation.preg_* calls (e.g., in monolithic controllers or Blade templates) may require significant effort.LogParser class) benefit without affecting the broader Laravel app.preg_* may resist the new API, requiring training or incentives (e.g., code reviews enforcing the wrapper).preg_* for critical paths (e.g., API request validation) using tools like PHPBench.Regex::raw() method to bypass the wrapper for edge cases, ensuring no regression in functionality.Strategic Alignment:
Validator::extend() vs. custom rules)? Should it replace or augment existing solutions?Adoption Roadmap:
Error Handling:
ValidationException)?Log::error()) for regex failures?Performance:
preg_match?)Maintenance:
Testing:
Alternatives:
Str::of(), Validator) suffice, or does this package fill a critical gap?StringUtil) that offer more features?Laravel Ecosystem Synergies:
preg_match in custom Rule classes or FormRequest validation with the package’s Regex::test() method.
use PHPStandardLibrary\Regex;
use Illuminate\Validation\Rule;
class SlugRule extends Rule {
public function passes($attribute, $value) {
return Regex::test('/^[a-z0-9\-]+$/', $value);
}
}
AuthorizesRequests or ValidatesRequests:
public function authorize() { ... }
public function rules() {
return [
'username' => ['required', new RegexRule('/^[a-z0-9\_\-]+$/')],
];
}
UserService::sanitizeInput()):
$cleaned = Regex::replace()
->pattern('/[^a-z0-9]/')
->with('')
->in($dirtyInput);
// In a service provider
Blade::directive('regex', function ($pattern) {
return "?? Regex::test({$pattern}, __oblivion);";
});
@regex('/^[A-Z0-9]+$/', $input) Valid @else Invalid @endregex
Collection with regex macros:
collect($strings)
->extract('pattern', '/\d{3}-\d{2}-\d{4}/')
->each(fn ($match) => Log::info($match));
$logs = Regex::extractAll()
->pattern('/ERROR: (.*)/')
->from(file_get_contents('storage/logs/laravel.log'));
Dependency Compatibility:
Phase 1: Proof of Concept (2–4 weeks)
preg_match with Regex::test().preg_replace with Regex::replace().Phase 2: Standardize API (4–6 weeks)
RegexRule, RegexValidator).laravel-regex-helpers) with:
RegexRule).@regex).extract()).Phase 3: Gradual Adoption (Ongoing)
How can I help you explore Laravel packages today?