PHP code example of simpleregex / srl-php

1. Go to this page and download the library: Download simpleregex/srl-php 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/ */

    

simpleregex / srl-php example snippets


$query = SRL::startsWith()
    ->anyOf(function (Builder $query) {
        $query->digit()
            ->letter()
            ->oneOf('._%+-');
    })->onceOrMore()
    ->literally('@')
    ->anyOf(function (Builder $query) {
        $query->digit()
            ->letter()
            ->oneOf('.-');
    })->onceOrMore()
    ->literally('.')
    ->letter()->atLeast(2)
    ->mustEnd()->caseInsensitive();

$srl = new SRL('literally "colo", optional "u", literally "r"');
preg_match($srl, 'color') // 1
$srl->isMatching('colour') // true
$srl->isMatching('soup') // false

preg_match($query, '[email protected]');

$query->isMatching('[email protected]'); // true
$query->isMatching('invalid-email.com'); // false

// Using SRL
$regEx = new SRL('literally "color:", whitespace, capture (letter once or more) as "color", literally "."');

// Using the query builder
$regEx = SRL::literally('color:')->whitespace()->capture(function (Builder $query) {
    $query->letter()->onceOrMore();
}, 'color')->literally('.');

$matches = $regEx->getMatches('Favorite color: green. Another color: yellow.');

echo $matches[0]->get('color'); // green
echo $matches[1]->get('color'); // yellow

// SRL:
new SRL('capture (literally "foo") if followed by (literally "bar")');

// Query Builder:
SRL::capture(function (Builder $query) {
    $query->literally('foo');
})->ifFollowedBy(function (Builder $query) {
    $query->literally('bar');
});

SRL::capture('foo')->ifFollowedBy(SRL::literally('bar'));