1. Go to this page and download the library: Download iak/regexbuilder library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
iak / regexbuilder example snippets
use RegexBuilder\Regex;
$string = "wow! this is cool!"
$match = Regex::word("wow")->symbol("!")->match($string); // wow!
$string = "This is a hard example!";
Regex::word("simple")->replace("simple", $string); // This is a simple example
Regex::word(["This", "simple", "example"])->matchAll($string); // ["this", "example"]
Regex::word()->matchAll($string) // ["this", "is", "a", "hard", "example"]
$string = "1 12 123 1234";
// Specify the exact count to match..
Regex::digit()->count(3)->match($string); // 123
// Or a minimum and maximum..
Regex::digit()->count(2,4)->matchAll($string); // [12, 123, 1234]
Regex::range("a", "z"); // a-z
// Using a callback
Regex::group(function ($builder) {
return $builder->range("a", "z");
});
// Using a raw pattern
Regex::group("a-z");
// Produces the same; [a-z]
$string = "Capture this if you can!";
// you can either capture the previous statement..
Regex::word("this")->capture();
// .. or using a callback
Regex::capture(function ($builder) {
return $builder->word("this");
});
// Produces the same; (this)
$string = "Do not capture this if you can!";
// you can either capture the previous statement..
Regex::word("this")->capture();
// .. or using a callback
Regex::capture(function ($builder) {
return $builder->word("this");
});
// Produces the same; (?:this)?
$string = "Capture this if you can";
Regex::startCapture()->word("this")->endCapture(); // (this)
$string = "important";
// Using a callback..
Regex::behind(function ($builder) {
return $builder->symbols("");
})
->word()
->match($string);
// .. or a raw pattern..
Regex::behind("\*\*\*\*")->word()->match($string);
// important
$string = "Is it spelled color or colour?";
// Using a characters
Regex::word("colour")->optional("u")->matchAll($string); // ["color", "colour"]
// Using a start and a length
Regex::word("colour")->optional(4,1)->matchAll($string); // ["color", "colour"]
// Make last statement optinal
Regex::symbols("colo")->char("u")->optional()->symbols("r")->matchAll($string); // ["color", "colour"]