Skip to content

Latest commit

 

History

History
59 lines (46 loc) · 1.79 KB

README.md

File metadata and controls

59 lines (46 loc) · 1.79 KB

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
)