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

Reverse Regex Laravel Package

ilario-pierbattista/reverse-regex

Generate example strings from regular expressions in PHP—useful for test data for forms, databases, and regex validation. Includes lexer/parser and random generators, supports literals, groups, classes, ranges, and quantifiers (with some Unicode/PCRE limits).

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require ilario-pierbattista/reverse-regex
    

    Ensure your project uses PHP 8.1+ (Laravel 10+).

  2. Basic Usage:

    use ReverseRegex\Lexer;
    use ReverseRegex\Random\SimpleRandom;
    use ReverseRegex\Parser;
    use ReverseRegex\Generator\Scope;
    
    $lexer = new Lexer('[A-Z]{5}[0-9]{4}'); // Example: "ABCDE1234"
    $random = new SimpleRandom(12345); // Seed for reproducibility
    $parser = new Parser($lexer, new Scope(), new Scope());
    $result = $parser->parse()->getResult()->generate('', $random);
    
    echo $result; // Outputs: e.g., "XK9QJ7421"
    
  3. First Use Case: Generate test data for Laravel validation rules (e.g., regex:/^[A-Z]{5}[0-9]{4}$/):

    $regex = '[A-Z]{5}[0-9]{4}';
    $testData = collect(range(1, 10))->map(fn() => generateString($regex))->toArray();
    

Where to Look First

  • Examples: Study the provided examples (e.g., ausphone.php, auspostcode.php) for Laravel-relevant patterns.
  • Regex Support Table: Reference the README’s table to validate your use case’s compatibility.
  • Changelog: Check 0.6.0 for PHP 8.1+ fixes (e.g., unbounded quantifiers).

Laravel Integration

  1. Service Provider: Register a facade or service container binding for reusable generation:

    // app/Providers/ReverseRegexServiceProvider.php
    public function register()
    {
        $this->app->singleton('regex.generator', function () {
            return new class {
                public function generate(string $regex, int $seed = null): string {
                    $lexer = new Lexer($regex);
                    $random = new SimpleRandom($seed ?? random_int(0, PHP_INT_MAX));
                    return (new Parser($lexer, new Scope(), new Scope()))
                        ->parse()
                        ->getResult()
                        ->generate('', $random);
                }
            };
        });
    }
    
  2. PestPHP Helper: Add a helper to tests/Pest.php:

    uses(Tests\TestCase::class)->in('*');
    uses(function () {
        $this->generateRegexString = function (string $regex, int $seed = null) {
            return app('regex.generator')->generate($regex, $seed);
        };
    });
    
  3. Factory Macro: Extend Laravel factories for seeded test data:

    // Database/Factories/UserFactory.php
    public function configure(): static
    {
        return $this->afterCreating(function (User $user) {
            $user->api_token = $this->generateRegexString('[A-Za-z0-9]{40}');
        });
    }
    

Implementation Patterns

Core Workflow

  1. Define Regex: Align with Laravel validation rules (e.g., regex:/^(?=.*[A-Z])(?=.*\d).{8,}$/ for passwords).

  2. Generate Data:

    $password = $this->generateRegexString('^(?=.*[A-Z])(?=.*\d).{8,}$');
    
  3. Reuse in Tests:

    it('validates complex passwords', function () {
        $password = $this->generateRegexString('^(?=.*[A-Z])(?=.*\d).{8,}$');
        $this->assertTrue(Str::is($password, '/^(?=.*[A-Z])(?=.*\d).{8,}$/'));
    });
    

Advanced Patterns

1. Seeded Generation for Reproducibility

Use a fixed seed in CI/CD to ensure consistent test data:

$seededData = $this->generateRegexString('[A-Z]{3}[0-9]{4}', 42);

2. Batch Generation

Generate arrays of test data for bulk operations:

$testEmails = collect(range(1, 50))->map(fn($i) =>
    $this->generateRegexString('[a-z]+@example\.com')
)->toArray();

3. Integration with Laravel Validation

Test validation rules dynamically:

$validator = Validator::make(['code' => $this->generateRegexString('[A-Z]{5}')], [
    'code' => 'regex:/^[A-Z]{5}$/',
]);
$validator->validate(); // Passes

4. Unicode and Hex Support

Generate locale-specific data (e.g., emojis, non-ASCII):

$emoji = $this->generateRegexString('\X{1F600}'); // 😀
$hexString = $this->generateRegexString('\x41-\x5A'); // Random uppercase letter

5. Custom Random Strategies

Extend SimpleRandom for domain-specific logic (e.g., biased distributions):

class BiasedRandom extends SimpleRandom {
    public function generate(int $min, int $max): int {
        // Custom logic (e.g., 70% chance for min value)
        return $min === 0 ? 0 : parent::generate($min, $max);
    }
}

6. Error Handling

Wrap generation in a try-catch for unsupported regex:

try {
    $data = $this->generateRegexString('\p{L}'); // Unsupported
} catch (\Exception $e) {
    $this->fail('Unsupported regex: ' . $e->getMessage());
}

Laravel-Specific Tips

1. PestPHP Data Providers

Use generateRegexString in data providers:

$dataProvider = [
    'valid' => [$this->generateRegexString('[A-Z]{3}')],
    'invalid' => [$this->generateRegexString('[a-z]{3}')],
];

2. Database Seeding

Seed tables with regex-compliant data:

// database/seeders/UsersTableSeeder.php
User::factory()->count(100)->create()->each(function ($user) {
    $user->update(['license_key' => $this->generateRegexString('[A-F0-9]{16}')]);
});

3. API Testing

Generate realistic API payloads:

$payload = [
    'token' => $this->generateRegexString('[A-Za-z0-9]{64}'),
    'metadata' => json_encode([
        'id' => $this->generateRegexString('[0-9]{10}'),
    ]),
];

4. Migration Testing

Test migrations with edge-case data:

$maxLengthString = $this->generateRegexString('[a-z]{255}');
Schema::create('test_table', function (Blueprint $table) {
    $table->string('field', 255)->unique();
});

Gotchas and Tips

Pitfalls

  1. Unsupported Regex Features:

    • Avoid: \p{L} (Unicode properties), lookarounds, backreferences.
    • Workaround: Pre-process regex to remove unsupported parts or use simpler patterns.
  2. Unbounded Quantifiers (*, +):

    • Issue: May generate excessively long strings (up to PHP_INT_MAX).
    • Fix: Use explicit ranges (e.g., {0,10}) or limit with SimpleRandom overrides.
  3. Escaping Meta-Characters:

    • Gotcha: Forgetting to escape regex meta-characters (e.g., ., *) in the input string.
    • Tip: Use preg_quote() for dynamic regex strings:
      $dynamicRegex = preg_quote($userInput, '/') . '[0-9]{4}';
      
  4. Unicode Limitations:

    • Issue: \X{####} requires valid Unicode codepoints; invalid ranges throw errors.
    • Debug: Validate codepoints with preg_match('/^\X{####}$/', $codepoint).
  5. Performance with Complex Regex:

    • Problem: Nested groups or high quantifiers (e.g., {1,1000}) slow generation.
    • Optimization:
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport