public function testCreateNull() { $delim = RegEx::DELIMITER; $empty = $delim . $delim; $regex = new RegEx(); $this->assertEquals($empty, $regex->getRegEx(), 'new Class(…)'); $regex = RegEx::make(); $this->assertEquals($empty, $regex->getRegEx(), 'Class::make(…) factory'); }
<?php require __DIR__ . '/../vendor/autoload.php'; use RegSexy\RegEx; $subject = <<<EOT 1. RegEx 2. RegExp 3. Regular Expression 4. Regular Expressions 5. RegularExpression 6. RegularExpressions EOT; // (RegExp|RegEx|Regular Expressions) // (Regular Expressions|RegExp|RegEx) // Reg(ular)*\s*Ex(p)*(ression)*(s)* $matches = RegEx::make('[0-9]\\. (RegEx|RegExp|Regular Expressions)')->matchAll($subject); while (!$matches->done) { echo "<li>"; var_dump($matches->next->match); }
<?php require __DIR__ . '/../vendor/autoload.php'; use RegSexy\Modifiers as mod; use RegSexy\RegEx; $subject = 'Waldo Dogsly and Fink 9 theo 10'; $pattern = '(waldo)* (\\w+ )+\\d+'; // by default no modifiers are used var_dump(RegEx::make($pattern)->match($subject)->match); // these 3 are equivalant var_dump(RegEx::make($pattern, 'iU')->match($subject)->match); var_dump(RegEx::make($pattern, mod::CASELESS, mod::UNGREEDY)->match($subject)->match); var_dump(RegEx::make($pattern)->caseless()->ungreedy()->match($subject)->match);
<?php require __DIR__ . '/../vendor/autoload.php'; use RegSexy\RegEx; // Clean up some phone numbers $before = "101 454 3567, 2024543567, (303)454-3567, 404-454-3567, 505.454.3567"; $after = RegEx::make('[(]{0,1} (\\d{3}) [)-\\.\\s]{0,1} (\\d{3}) [-\\.\\s]{0,1} (\\d{4})')->extended()->replace($before, '($1) $2-$3'); var_dump($before); var_dump($after);
<?php require __DIR__ . '/../vendor/autoload.php'; use RegSexy\RegEx; // Here's our test subject, a sentence about animals $subject = 'Cats are pretty funny I love, that, what is called… "ICanHasCheezburger?"!, ' . "is pretty great… but *really* I'm more of a dog person. Got two little Chihuahuas at home!"; // Create a (sexy) RegEx object $someCats = new RegEx('(Chesire|Tabby|Siamese|ICanHasCheezburger\\?)'); $firstCat = $someCats->match($subject); if ($firstCat) { var_dump($firstCat); } else { die('shiiiit son'); } // If you use the expression once you can use fluent syntax // var_dump((new RegEx('(Chihuahua|Dachsund|Corgi)s'))->match($subject)->match); // the "make()" factory makes this even clenaer var_dump(RegEx::make('(Chihuahua|Dachsund|Corgi)s')->match($subject)->match); // the Match object even implements __toString() echo RegEx::make('(Chihuahua|Dachsund|Corgi)s')->match($subject);
<?php require __DIR__ . '/../vendor/autoload.php'; use RegSexy\Modifiers as m; use RegSexy\RegEx; // If you've ever worked with PHP's native regular expression syntax (i.e. the preg_* functions) // you're accustomed to blocks like this: $pattern = "/f..k th../iu"; preg_match($pattern, 'FRÅK THIS', $match); var_dump($match[0]); preg_match($pattern, 'førk them', $match); var_dump($match[0]); // A pattern is a string composed of 3 compoennts: // 1. a delimiter like '/', the slash is conventional, '~' and '#' are also common // 2. the pattern like 'f..k th..', the engine of your creation! // 3. and optional modifiers like 'iu', which change some properties of the regex engine $pattern = "/f..k th../"; $pattern = "~f..k th..~iu"; $pattern = "#f..k th..#u"; // With RegSexy you get cleaner declerations // • No delimiters. They are added and escaped for you, no futzing. $match = RegEx::make('f..k th..')->setModifiers('iU')->match('FÜNK THAT'); var_dump($match); // To specifiy your modifiers you have a couple of options. // You can simply pass a string as the second variable $match = RegEx::make('f..k th..', 'iu')->match('fîrk THUR')->match; var_dump($match); // Or you can 'use' the modifier constants (which just hold strings) // pass each invididually $match = RegEx::make('f..k th..', m::CASELESS, m::UTF8)->match('Fink Thus')->match; var_dump($match);
<?php require __DIR__ . '/../vendor/autoload.php'; use RegSexy\RegEx; echo "<pre>"; // Just a test string $subject = 'Cats are pretty funny I love, that, what is called… "ICanHasCheezburger?"!, ' . 'is pretty great… but *really* I\'m more of a dog person. Got 2 little Chihuahuas at home!'; // match() returns a Match object $match = RegEx::make('(Chihuahuas|ICanHasCheezburger\\?)')->match($subject); var_dump($subject); // matchAll() returns a MatchList object, which is a basic itterator $matches = RegEx::make('(Chihuahuas|ICanHasCheezburger\\?)')->matchAll($subject); foreach ($matches as $index => $v) { echo "match " . ++$index . ": {$v}\n"; } // You can grab a paticular item var_dump($matches->match(1)->match); // MatchList also implements __toString(), nice for quick testing echo RegEx::make('(Chihuahuas|ICanHasCheezburger\\?)')->matchAll($subject);