icomefromthenet/reverse-regex
Generate sample strings from regular expressions for test data and validation. ReverseRegex parses a supported subset of regex syntax (literals, groups, character classes, quantifiers, escapes, some Unicode via \X{####}) and outputs randomized matching text via PHP generators.
FormRequest rules).regex: rule) for test data generation.\p{} (Unicode properties), lookarounds, and backreferences—may break complex patterns.*, +) could cause infinite loops or memory issues.doctrine/lexer, patchwork/utf8).beforeEach blocks for reusable fake data.// config/testing.php
'generators' => [
'regex' => \App\Services\ReverseRegexGenerator::class,
],
(a|b){2,}(c|d){1,3}).preg_match) may be required for critical paths.\p{} Support: Workarounds (e.g., \X{} hex ranges) limit multilingual use cases.{1,10} instead of *).Faker or hardcoded values?\p{L}) that are critical?preg_match)?Faker extensions) that could fill gaps?phpunit, Pest, and Laravel factories.Phase 1: Proof of Concept (PoC)
[a-z]{5}, \d{10}) to validate output.Faker for equivalent use cases.$generator = new \ReverseRegex\Generator(
new \ReverseRegex\Parser(
new \ReverseRegex\Lexer('[a-z]{5}'),
new \ReverseRegex\Scope(),
new \ReverseRegex\Scope()
),
new \ReverseRegex\Random\SimpleRandom()
);
$fakeString = $generator->generate();
Phase 2: Laravel Integration
// app/Providers/ReverseRegexServiceProvider.php
public function register() {
$this->app->singleton(ReverseRegexGenerator::class, function ($app) {
return new \App\Services\ReverseRegexGenerator(
new \ReverseRegex\Lexer($app['config']['regex.pattern']),
new \ReverseRegex\Random\SimpleRandom()
);
});
}
// app/Helpers/RegexHelper.php
function generateFromRegex(string $pattern, int $seed = null): string {
$lexer = new \ReverseRegex\Lexer($pattern);
$gen = new \ReverseRegex\Random\SimpleRandom($seed);
return (new \ReverseRegex\Parser($lexer, new \ReverseRegex\Scope(), new \ReverseRegex\Scope()))
->parse()
->getResult()
->generate('', $gen);
}
Phase 3: Testing Framework Adoption
public function testEmailValidation() {
$fakeEmail = generateFromRegex('[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}');
$this->assertMatchesRegularExpression('/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/', $fakeEmail);
}
beforeEach(function () {
$this->fakeData = generateFromRegex('[a-z]{10}');
});
it('generates valid test data', function () {
expect($this->fakeData)->toMatchRegex('/^[a-z]{10}$/');
});
// database/factories/UserFactory.php
public function definition() {
return [
'username' => generateFromRegex('[a-z0-9]{8,20}'),
'email' => generateFromRegex('[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}'),
];
}
\d, \w), and Unicode ranges (\X{}).\p{}, lookarounds, backreferences, and some quantifier edge cases.\p{L} with \X{0041}-\X{007A} (ASCII letters) or \X{0080}-\X{FFFF} (extended Unicode).*/+; use {1,10} for bounded generation.doctrine/lexer and patchwork/utf8 are dependencies.["test@example.com", "user123"] → dynamic generation).How can I help you explore Laravel packages today?