PHP code example of damilarekoiki / phpregex

1. Go to this page and download the library: Download damilarekoiki/phpregex library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

damilarekoiki / phpregex example snippets




use DamilareKoiki\PhpRegex\Regex;

$regex = Regex::build()
    ->beginsWith('INV')
    ->then('-')
    ->digits()
    ->endsWith('.pdf');

if ($regex->matches('INV-12345.pdf')) {
    // It's a match!
}

$regex = Regex::build()
    ->containsDigit()
    ->containsUppercaseLetter()
    ->containsAtLeastSequencesOf(fn(Regex $regex) => $regex->anything(), 8);

$regex->matches('Pass1234'); // true

$regex = Regex::build()->digits(); // A consecutive sequence of digits

// Count matches
$count = $regex->count('There are 12 apples and 34 oranges.'); // 2

// Replace matches
$clean = $regex->replace('Order #12345', 'XXXXX'); // "Order #XXXXX"

// Using callbacks for replacement
$result = $regex->replace('Score: 10', function(array $match): int {
    return (int)$match[0] * 2;
}); // "Score: 20"

$regex = Regex::build()->containsSequence(function (Sequence $sequence): void {
    $sequence->then('Step 1')
      ->then(fn(Regex $regex) => $regex->digits())
      ->then('Finished');
});

// ❌ This will insert 'bar' at the start of the string if 'foo' exists anywhere.
Regex::build()->contains('foo')->replace('I have foo', 'bar');

// ✅ This will replace 'foo' with 'bar'.
Regex::build()->then('foo')->replace('I have foo', 'bar');