{"id":13828394,"url":"https://github.com/spatie/regex","last_synced_at":"2025-05-14T14:02:07.439Z","repository":{"id":10491122,"uuid":"65915598","full_name":"spatie/regex","owner":"spatie","description":"A sane interface for php's built in preg_* functions","archived":false,"fork":false,"pushed_at":"2022-12-05T14:16:00.000Z","size":91,"stargazers_count":1116,"open_issues_count":0,"forks_count":46,"subscribers_count":11,"default_branch":"main","last_synced_at":"2025-04-11T06:13:00.587Z","etag":null,"topics":["hacktoberfest","php","regex","regular-expression"],"latest_commit_sha":null,"homepage":"https://freek.dev/549-a-package-to-easily-work-with-regex-in-php","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"spatie","custom":"https://spatie.be/open-source/support-us"}},"created_at":"2016-08-17T14:46:26.000Z","updated_at":"2025-03-24T01:10:34.000Z","dependencies_parsed_at":"2023-01-13T15:58:31.776Z","dependency_job_id":null,"html_url":"https://github.com/spatie/regex","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fregex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fregex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fregex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fregex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/regex/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254159140,"owners_count":22024558,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","php","regex","regular-expression"],"created_at":"2024-08-04T09:02:44.468Z","updated_at":"2025-05-14T14:02:07.416Z","avatar_url":"https://github.com/spatie.png","language":"PHP","readme":"\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/support-ukraine.svg?t=1\" /\u003e](https://supportukrainenow.org)\n\n# Making regex great again\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/regex.svg?style=flat-square)](https://packagist.org/packages/spatie/regex)\n![Tests](https://github.com/spatie/regex/workflows/Tests/badge.svg)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/regex.svg?style=flat-square)](https://packagist.org/packages/spatie/regex)\n\nPhp's built in `preg_*` functions require some odd patterns like passing variables by reference and treating `false` or `null` values as errors. `spatie/regex` provides a cleaner interface for `preg_match`, `preg_match_all`, `preg_replace` and `preg_replace_callback`.\n\n```php\nuse Spatie\\Regex\\Regex;\n\n// Using `match`\nRegex::match('/a/', 'abc'); // `MatchResult` object\nRegex::match('/a/', 'abc')-\u003ehasMatch(); // true\nRegex::match('/a/', 'abc')-\u003eresult(); // 'a'\n\n// Capturing groups with `match`\nRegex::match('/a(b)/', 'abc')-\u003eresult(); // 'ab'\nRegex::match('/a(b)/', 'abc')-\u003egroup(1); // 'b'\n\n// Setting defaults\nRegex::match('/a(b)/', 'xyz')-\u003eresultOr('default'); // 'default'\nRegex::match('/a(b)/', 'xyz')-\u003egroupOr(1, 'default'); // 'default'\n\n// Using `matchAll`\nRegex::matchAll('/a/', 'abcabc')-\u003ehasMatch(); // true\nRegex::matchAll('/a/', 'abcabc')-\u003eresults(); // Array of `MatchResult` objects\n\n// Using replace\nRegex::replace('/a/', 'b', 'abc')-\u003eresult(); // 'bbc';\nRegex::replace('/a/', function (MatchResult $result) {\n    return $result-\u003eresult() . 'Hello!';\n}, 'abc')-\u003eresult(); // 'aHello!bc';\n```\n\nSpatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/regex.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/regex)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Installation\n\nYou can install the package via composer:\n\n``` bash\ncomposer require spatie/regex\n```\n\n## Usage\n\n### Matching a pattern once\n\nMatches a pattern on a subject. Returns a `MatchResult` object for the first match.\n\n```php\n/**\n * @param string $pattern\n * @param string $subject\n *\n * @return \\Spatie\\Regex\\MatchResult\n */\nRegex::match(string $pattern, string $subject): MatchResult\n```\n\n#### `MatchResult::hasMatch(): bool`\n\nChecks if the pattern matches the subject.\n\n```php\nRegex::match('/abc/', 'abc')-\u003ehasMatch(); // true\nRegex::match('/def/', 'abc')-\u003ehasMatch(); // false\n```\n\n#### `MatchResult::result(): string`\n\nReturn the full match that was made. Returns `null` if no match was made.\n\n```php\nRegex::match('/abc/', 'abc')-\u003eresult(); // 'abc'\nRegex::match('/def/', 'abc')-\u003eresult(); // null\n```\n\n#### `MatchResult::group(int $id): string`\n\nReturn the contents of a captured group (with a 1-based index). Throws a `RegexFailed` exception if the group doesn't exist.\n\n```php\nRegex::match('/a(b)c/', 'abc')-\u003egroup(1); // 'b'\nRegex::match('/a(b)c/', 'abc')-\u003egroup(2); // `RegexFailed` exception\n```\n\n### Matching all occurences of a pattern\n\nMatches a pattern on a subject. Returns a `MatchAllResult` object containing all matches.\n\n```php\n/**\n * @param string $pattern\n * @param string $subject\n *\n * @return \\Spatie\\Regex\\MatchAllResult\n */\npublic static function matchAll(string $pattern, string $subject): MatchAllResult\n```\n\n#### `MatchAllResult::hasMatch(): bool`\n\nChecks if the pattern matches the subject.\n\n```php\nRegex::matchAll('/abc/', 'abc')-\u003ehasMatch(); // true\nRegex::matchAll('/abc/', 'abcabc')-\u003ehasMatch(); // true\nRegex::matchAll('/def/', 'abc')-\u003ehasMatch(); // false\n```\n\n#### `MatchAllResult::results(): array`\n\nReturns an array of `MatchResult` objects.\n\n```php\n$results = Regex::matchAll('/ab([a-z])/', 'abcabd')-\u003eresults();\n\n$results[0]-\u003eresult(); // 'abc'\n$results[0]-\u003egroup(1); // 'c'\n$results[1]-\u003eresult(); // 'abd'\n$results[1]-\u003egroup(1); // 'd'\n```\n\n### Replacing a pattern in a subject\n\nReplaces a pattern in a subject. Returns a `ReplaceResult` object.\n\n```php\n/**\n * @param string|array $pattern\n * @param string|array|callable $replacement\n * @param string|array $subject\n * @param int $limit\n *\n * @return \\Spatie\\Regex\\ReplaceResult\n */\npublic static function replace($pattern, $replacement, $subject, $limit = -1): ReplaceResult\n```\n\n#### `ReplaceResult::result(): mixed`\n\n```php\nRegex::replace('/a/', 'b', 'abc')-\u003eresult(); // 'bbc'\n```\n\n`Regex::replace` also works with callables. The callable will receive a `MatchResult` instance as it's argument.\n\n```php\nRegex::replace('/a/', function (MatchResult $matchResult) {\n    return str_repeat($matchResult-\u003eresult(), 2);\n}, 'abc')-\u003eresult(); // 'aabc'\n```\n\nPatterns, replacements and subjects can also be arrays. `Regex::replace` behaves exactly like [`preg_replace`](http://php.net/manual/en/function.preg-replace.php) in those instances.\n\n### Error handling\n\nIf anything goes wrong in a `Regex` method, a `RegexFailed` exception gets thrown. No need for checking `preg_last_error()`.\n\n## Testing\n\n``` bash\n$ composer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Postcardware\n\nYou're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.\n\nOur address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.\n\nWe publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).\n\n## Credits\n\n- [Sebastian De Deyne](https://github.com/sebastiandedeyne)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","funding_links":["https://github.com/sponsors/spatie","https://spatie.be/open-source/support-us"],"categories":["PHP"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fregex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Fregex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fregex/lists"}