Quantcast
Channel: Laravel News
Viewing all articles
Browse latest Browse all 1813

Simple Regex Language is aiming to simplify Regular Expressions

$
0
0

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.


Viewing all articles
Browse latest Browse all 1813

Trending Articles