PHP code example of darshphpdev / easyregex

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

    

darshphpdev / easyregex example snippets


'providers' => [
    // ...
    DarshPhpDev\EasyRegex\\EasyRegexServiceProvider::class,
];

// In your controller
// Use The Helper class RegexBuilder to build and validate regex
use RegexBuilder;

$regex = RegexBuilder::make()->startsWith()->literally('abc')
					->anyLetterOf('A', 'B', 'C')
					->zeroOrMore()
			    		->where(function ($query) {
					        $query->digit()->smallLetter();
					    })->toRegexString();
// the above example will return the regex string /^abc[ABC]*([0-9][a-z])/

// you can also match your string against the generated regex
$regexObject = RegexBuilder::make()->startsWith()->literally('abc')
					->anyLetterOf('A', 'B', 'C')
					->zeroOrMore()
			    		->where(function ($query) {
					        $query->digit()->smallLetter();
					    });
$isMatching = $regexObject->match('abcAAA5g');		// $isMatching = true
$isMatching = $regexObject->match('abdd5g');		// $isMatching = false

// you can also specify the wrapping symbol as an argument to the make() function
// default is '/'
$regex = RegexBuilder::make('#')->literally('a')
				->letterPatternOf('ABC')
				->zeroOrMore()
	    			->toRegexString();
// the above example will return the regex string #a(ABC)*#

// you can also use the global helper regexBuilder()
$regex = regexBuilder()->make()->digit()
			->capitalLetters('A', 'F')
			->onceOrMore()
			->toRegexString();			    		
// the above example will return the regex string /[0-9][A-F]+/
// FOR FULL USAGE, SEE BELOW..

$regex = RegexBuilder::make()->literally('Regex')->toRegexString();
// $regex = /Regex/
$regex = RegexBuilder::make()->anyLetterOf('a', 'b', 'c')->toRegexString();
// $regex = /[abc]/
$regex = RegexBuilder::make()->exceptLetterOf('c')->toRegexString();
// $regex = /[^c]/
$regex = RegexBuilder::make()->letterPatternOf('july')->toRegexString();
// $regex = /(july)/
$regex = RegexBuilder::make()->digit()->anyChar()->toRegexString();
// $regex = /[0-9]./
$regex = RegexBuilder::make()->anyChar()->atMost(5)->toRegexString();
// $regex = /.{5}/
$regex = RegexBuilder::make()->exceptSmallLetters('u', 'z')->toRegexString();
// $regex = /[^u-z]/
$regex = RegexBuilder::make()->capitalLetters('A', 'F')->toRegexString();
// $regex = /[A-F]/
$regex = RegexBuilder::make()->rawRegex('a(ABC)*')->match('aABCABCABC');
// $regex = true
$regex = RegexBuilder::make()->rawRegex('a(ABC)*')->match('aABCABCA');
// $regex = false

// and so on..