Skip to content

rkrx/php-regexp-builder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

php-regexp-builder

Human-friendly regular expression authoring

SensioLabsInsight Build Status Scrutinizer Code Quality Packagist

<?php

/* ... */

$invalidEndingCharacters = (new RegExpBuilder)
->expectAnyOf('.-')
->once();

$namePattern = (new RegExpBuilder)
->assertNotPrecededBy($invalidEndingCharacters)
->expectAnyOf(['._-+', new AnyLetter, new AnyDigit])->onceOrMore()
->assertNotFollowedBy($invalidEndingCharacters);

$domainPattern = (new RegExpBuilder)
->assertNotPrecededBy($invalidEndingCharacters)
->expectAnyOf(['.-', new AnyLetter, new AnyDigit])->onceOrMore()
->assertNotFollowedBy($invalidEndingCharacters);

$pattern = (new RegExpBuilder)
->lineStart()
->group($namePattern, 'a')->once()
->expect('@')->once()
->group($domainPattern, 'b')->once()
->lineEnd()
->compile();

echo "{$pattern}\n";

$res = $pattern->test('[email protected]');
var_dump($res); // true

$res = $pattern->getGroups('[email protected]');
print_r($res);

Output:

/^(?P<a>(?<![\.\-])[\._\-\+\p{L}\d]+?(?![\.\-]))(?:@)(?P<b>(?<![\.\-])[\.\-\p{L}\d]+?(?![\.\-]))$/u
bool(true)
Array
(
    [a] => max.mustermann+github
    [0] => max.mustermann+github
    [b] => googlemail.com
    [1] => googlemail.com
)

About

Human-friendly regular expression authoring

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages