### Installation Source: https://github.com/composer/pcre/blob/main/README.md Installs the latest version of the composer/pcre library using Composer. ```bash composer require composer/pcre ``` -------------------------------- ### Regex Replacement Examples Source: https://github.com/composer/pcre/blob/main/README.md Demonstrates various ways to use the Regex::replace and Regex::replaceCallback methods for string manipulation using regular expressions. Includes simple replacement, replacement with a callback function, and replacement using an array of callbacks. ```PHP $newString = Regex::replace('{fo+}', 'bar', $string)->result; $newString = Regex::replaceCallback('{fo+}', function ($match) { return strtoupper($match[0]); }, $string)->result; $newString = Regex::replaceCallbackArray(['{fo+}' => fn ($match) => strtoupper($match[0])], $string)->result; ``` -------------------------------- ### Basic Usage Comparison Source: https://github.com/composer/pcre/blob/main/README.md Compares the traditional usage of PHP's preg_* functions with the type-safe alternatives provided by the Composer PCRE library's Preg class. ```php use Composer\Pcre\Preg; // Traditional usage: // if (preg_match('{fo+}', $string, $matches)) { ... } // if (preg_match('{fo+}', $string, $matches, PREG_OFFSET_CAPTURE)) { ... } // if (preg_match_all('{fo+}', $string, $matches)) { ... } // $newString = preg_replace('{fo+}', 'bar', $string); // $newString = preg_replace_callback('{fo+}', function ($match) { return strtoupper($match[0]); }, $string); // $newString = preg_replace_callback_array(['{fo+}' => fn ($match) => strtoupper($match[0])], $string); // $filtered = preg_grep('{[a-z]}', $elements); // $array = preg_split('{[a-z]+}', $string); // Composer PCRE library usage: if (Preg::match('{fo+}', $string, $matches)) { ... } if (Preg::matchWithOffsets('{fo+}', $string, $matches)) { ... } if (Preg::matchAll('{fo+}', $string, $matches)) { ... } $newString = Preg::replace('{fo+}', 'bar', $string); $newString = Preg::replaceCallback('{fo+}', function ($match) { return strtoupper($match[0]); }, $string); $newString = Preg::replaceCallbackArray(['{fo+}' => fn ($match) => strtoupper($match[0])], $string); $filtered = Preg::grep('{[a-z]}', $elements); $array = Preg::split('{[a-z]+}', $string); ``` -------------------------------- ### Boolean Match Methods Source: https://github.com/composer/pcre/blob/main/README.md Demonstrates the use of `isMatch` and `isMatchAll` methods from the Composer PCRE library's Preg class, which return a boolean indicating if a match occurred. ```php use Composer\Pcre\Preg; if (Preg::isMatch('{fo+}', $string, $matches)) // bool if (Preg::isMatchAll('{fo+}', $string, $matches)) // bool ``` -------------------------------- ### Regex Class Usage Source: https://github.com/composer/pcre/blob/main/README.md Showcases the `Regex` class from the Composer PCRE library, which provides result objects for match operations, offering a more verbose but structured approach. ```php use Composer\Pcre\Regex; // this is useful when you are just interested in knowing if something matched // as it returns a bool instead of int(1/0) for match $bool = Regex::isMatch('{fo+}', $string); $result = Regex::match('{fo+}', $string); if ($result->matched) { something($result->matches); } $result = Regex::matchWithOffsets('{fo+}', $string); if ($result->matched) { something($result->matches); } $result = Regex::matchAll('{fo+}', $string); if ($result->matched && $result->count > 3) { something($result->matches); } ``` -------------------------------- ### Strict Groups Match Methods Source: https://github.com/composer/pcre/blob/main/README.md Illustrates the usage of `matchStrictGroups` and `matchAllStrictGroups` from the Composer PCRE library's Preg class, ensuring match groups are always present and non-nullable. ```php use Composer\Pcre\Preg; // $matches is guaranteed to be an array of strings, if a subpattern does not match and produces a null it will throw if (Preg::matchStrictGroups('{fo+}', $string, $matches)) if (Preg::matchAllStrictGroups('{fo+}', $string, $matches)) ``` -------------------------------- ### PREG_UNMATCHED_AS_NULL Behavior Comparison Source: https://github.com/composer/pcre/blob/main/README.md Illustrates the difference in match results when using PREG_UNMATCHED_AS_NULL compared to not using the flag. This highlights the library's consistent use of this flag for predictable and safer match outcomes. ```PHP preg_match('/(a)(b)*(c)(d)*/', 'ac', $matches, $flags); --- | no flag | PREG_UNMATCHED_AS_NULL | | --- | --- | | array (size=4) | array (size=5) | | 0 => string 'ac' (length=2) | 0 => string 'ac' (length=2) | | 1 => string 'a' (length=1) | 1 => string 'a' (length=1) | | 2 => string '' (length=0) | 2 => null | | 3 => string 'c' (length=1) | 3 => string 'c' (length=1) | | | 4 => null | ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.