s9e/regexp-builder
Generate compact regular expressions that match a given list of strings (ideal for keyword search). Builds optimized patterns like ba[rz]|foo from input terms, with factories for PHP, Java, JavaScript, RE2, plus RawBytes/RawUTF8 output modes.
FormRequest, Validator), query builders (e.g., dynamic whereRaw clauses), and scoped search (e.g., Eloquent query filters).RegexpBuilder), minimal footprint (~10KB).PHPUnit or standalone PHP tests.Cache facade) if used repeatedly.whereRaw), sanitize inputs rigorously.Str::contains or Str::is suffice for simpler cases?use s9e\RegexpBuilder\RegexpBuilder;
use Illuminate\Validation\Rule;
$builder = new RegexpBuilder();
$regex = $builder->addStrings(['valid@example.com', 'user@test.org'])->getRegex();
$rules = ['email' => ['required', Rule::regex($regex)]];
$query->whereRaw("column REGEXP ?", [$builder->getRegex()]);
FormRequest validation or middleware.RegexpBuilder for consistency:
class DynamicRegexValidator {
public static function validate(array $strings, string $input): bool {
return preg_match((new RegexpBuilder())->addStrings($strings)->getRegex(), $input);
}
}
laravel/framework, symfony/http-foundation).FormRequest).\Log::debug('Dynamic regex:', ['regex' => $builder->getRegex()]);
preg_last_error() to catch regex compilation failures.$regex = Cache::remember("regex_{$cacheKey}", now()->addHours(1), fn() =>
(new RegexpBuilder())->addStrings($strings)->getRegex()
);
| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Malicious input generates ReDoS | Application hangs/crashes | Input length/character whitelisting |
| Regex compilation errors | Silent failures or 500 errors | Wrap in try-catch; fallback to static regex |
| Large string lists | Performance degradation | Implement size limits; optimize regex |
| Package abandonment | Security/feature gaps | Fork or migrate to alternative |
How can I help you explore Laravel packages today?