Simple Regex Language is aiming to simplify Regular Expressions
Development Tools / August 30, 2016

Simple Regex Language is aiming to simplify Regular Expressions

Simple Regex Language is a new PHP package that aims to simplify regular expressions using a cleaner written word approach to the syntax. Here is an example of validating an email address:

begin with either of (number, letter, one of "._%+-") once or more,
literally "@",
either of (number, letter, one of ".-") once or more,
literally ".",
letter at least 2 times,
must end, case insensitive

Which parses into the following expression:

/^(?:[0-9]|[a-z]|[\._%\+-])+(?:@)(?:[0-9]|[a-z]|[\.-])+(?:\.)[a-z]{2,}$/i

Or if you prefer a PHP flavored approach:

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

You can find more details on the SRL and find all the code on GitHub.

Laravel News Partners

Newsletter

Join the weekly newsletter and never miss out on new tips, tutorials, and more.