{"id":27185950,"url":"https://github.com/bdusell/jitsu-regex","last_synced_at":"2025-08-30T10:39:32.710Z","repository":{"id":56999482,"uuid":"42755646","full_name":"bdusell/jitsu-regex","owner":"bdusell","description":"A better API for PHP's regular expression functions","archived":false,"fork":false,"pushed_at":"2016-05-07T07:49:36.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-14T01:18:23.266Z","etag":null,"topics":["helper-functions","php","regex","regular-expression"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bdusell.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-09-19T02:05:24.000Z","updated_at":"2017-10-17T21:19:39.000Z","dependencies_parsed_at":"2022-08-21T13:20:48.373Z","dependency_job_id":null,"html_url":"https://github.com/bdusell/jitsu-regex","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/bdusell/jitsu-regex","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdusell%2Fjitsu-regex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdusell%2Fjitsu-regex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdusell%2Fjitsu-regex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdusell%2Fjitsu-regex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bdusell","download_url":"https://codeload.github.com/bdusell/jitsu-regex/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdusell%2Fjitsu-regex/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272839640,"owners_count":25001861,"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","status":"online","status_checked_at":"2025-08-30T02:00:09.474Z","response_time":77,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["helper-functions","php","regex","regular-expression"],"created_at":"2025-04-09T17:55:33.766Z","updated_at":"2025-08-30T10:39:32.678Z","avatar_url":"https://github.com/bdusell.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"jitsu/regex\n------------\n\nThe `Jitsu\\RegexUtil` class is a collection of static methods for dealing with\nregular expressions in PHP. These helper functions simplify the creation of new\npatterns, escaping literal strings, handling errors, and accessing match\noffsets.\n\nThis package is part of [Jitsu](https://github.com/bdusell/jitsu).\n\n## Installation\n\nInstall this package with [Composer](https://getcomposer.org/):\n\n```sh\ncomposer require jitsu/regex\n```\n\n## Namespace\n\nThe class is defined under the namespace `Jitsu`.\n\n## API\n\n### class Jitsu\\\\RegexUtil\n\nA collection of static methods for dealing with regular expressions.\n\n#### RegexUtil::create($pat, $flags = '', $start = null, $end = null)\n\nCreate a regular expression from a PCRE pattern.\n\nThis converts a string containing a PCRE pattern to a value\ncompatible for use with this module. Do not include the delimiters;\nthey will be added and escaped automatically. You can specify any\nflags in a separate string. If you know that your PCRE is already\nproperly escaped with respect to a certain set of deliters, you can\noptionally provide the start and ending delimiters to use and avoid\nthe overhead of escaping the pattern.\n\n|   | Type | Description |\n|---|------|-------------|\n| **`$pat`** | `string` | A PCRE pattern with *no* delimiters. |\n| **`$flags`** | `string` | Optional PCRE flags. |\n| **`$start`** | `string|null` | An optional start delimiter to use. |\n| **`$end`** | `string|null` | An optional end delimiter to use. Defaults to the start delimiter. |\n| returns | `string` |  |\n\n#### RegexUtil::errorString($code)\n\nGet an error string for a `PREG_` error code.\n\n|   | Type | Description |\n|---|------|-------------|\n| **`$code`** | `int` | A `PREG_` error code. |\n| returns | `string` |  |\n\n#### RegexUtil::match($regex, $str, $offset = 0)\n\nTry to match a regular expression against a string.\n\nTests a regular expression against a string and returns a\n`RegexUtilMatch` object, or `null` if there was no match.\n\nThe match at index 0 is the part of the string which matched the\nwhole pattern.\n\n|   | Type | Description |\n|---|------|-------------|\n| **`$regex`** | `string` | The regular expression. |\n| **`$str`** | `string` | The string. |\n| **`$offset`** | `int` | An optional starting offset. |\n| returns | `\\Jitsu\\RegexUtilMatch|null` |  |\n| throws | `\\RuntimeException` | Thrown if the regular expression is not valid. |\n\n#### RegexUtil::matchWithOffsets($regex, $str, $offset = 0)\n\nLike `match`, but also include the starting indices of the matches.\n\nIf the string matches the regular expression, the return value\nincludes the starting indices of the matches as well. A starting\nindex of -1 indicates that the group was not matched.\n\n|   | Type |\n|---|------|\n| **`$regex`** | `string` |\n| **`$str`** | `string` |\n| **`$offset`** | `int` |\n| returns | `\\Jitsu\\RegexUtilMatch|null` |\n| throws | `\\RuntimeException` |\n\n#### RegexUtil::matchAll($regex, $str, $offset = 0)\n\nGet all non-overlapping matches of a regular expression in a string.\n\n|   | Type |\n|---|------|\n| **`$regex`** | `string` |\n| **`$str`** | `string` |\n| **`$offset`** | `int` |\n| returns | `\\Jitsu\\RegexUtilMatch[]` |\n| throws | `\\RuntimeException` |\n\n#### RegexUtil::matchAllWithOffsets($regex, $str, $offset = 0)\n\nGet all non-overlapping matches of a regular expression in a string\nalong with offset information.\n\n|   | Type |\n|---|------|\n| **`$regex`** | `string` |\n| **`$str`** | `string` |\n| **`$offset`** | `int` |\n| returns | `\\Jitsu\\RegexUtilMatch[]` |\n| throws | `\\RuntimeException` |\n\n#### RegexUtil::escape($str, $delim = null)\n\nEscape a string for interpolation in a regular expression.\n\nIf used with a pattern where the delimiter is being explicitly set,\nyou must provide that delimiter as the second argument.\n\n|   | Type |\n|---|------|\n| **`$str`** | `string` |\n| **`$delim`** | `string|null` |\n| returns | `string` |\n\n#### RegexUtil::replace($regex, $str, $replacement, $limit = null)\n\nReplace the portion of a string which matches a regular expression\nwith another string.\n\nThe replacement string may use backreferences in the form `\\n`,\n`$n`, or `${n}`. Optionally specify a limit for the number of\nreplacements which may be made (pass `null` for unlimited).\n\nStores the number of replacements made in the optional `$count`\nvariable.\n\nIf `$replacement` is not a string or array, it will be interpreted\nas a callback with the signature `function($matches)` whose return\nvalue will be used to generate the replacement strings. The\n`$matches` parameter is an array containing the matched groups.\n\nAny one of the arguments `$regex`, `$replacement`, or `$str` may\nbe an array of multiple values. Whenever each is a scalar, it\napplies to all the values in the other arguments, be they scalars or\narrays. Whenever each is an array, it applies pairwise to the other\narray arguments.\n\nWhen `$regex` is an array tested against a scalar `$str`, all of the\npatterns are tested as a logical \"or\".\n\nWhen `$replacement` is an array with too few elements for the other\narray arguments, the missing values are assumed to be the empty\nstring. If it is an array, it may contain only strings, not\ncallbacks.\n\nWhen `$str` is an array, an array of the replaced strings is\nreturned.\n\n|   | Type |\n|---|------|\n| **`$regex`** | `string|string[]` |\n| **`$str`** | `string|string[]` |\n| **`$replacement`** | `string|string[]|callable` |\n| **`$limit`** | `int|null` |\n| returns | `string|string[]` |\n| throws | `\\RuntimeException` |\n\n#### RegexUtil::replaceAndCount($regex, $str, $replacement, $limit = null)\n\nLike `replace`, but include the number of replacements as a second\nreturn value.\n\n|   | Type | Description |\n|---|------|-------------|\n| **`$regex`** | `string|string[]` |  |\n| **`$str`** | `string|string[]` |  |\n| **`$replacement`** | `string|string[]|callable` |  |\n| **`$limit`** | `int|null` |  |\n| returns | `array` | The pair `array($replaced, $count)`. |\n| throws | `\\RuntimeException` |  |\n\n#### RegexUtil::replaceWith($regex, $str, $callback, $limit = null)\n\nLike `replace`, except that the second parameter is always\ninterpreted as a callback.\n\nThis allows function names, etc. to be passed.\n\n|   | Type |\n|---|------|\n| **`$regex`** | `string|string[]` |\n| **`$str`** | `string|string[]` |\n| **`$callback`** | `callable` |\n| **`$limit`** | `int|null` |\n| returns | `string|string[]` |\n| throws | `\\RuntimeException` |\n\n#### RegexUtil::replaceAndCountWith($regex, $str, $callback, $limit = null)\n\nLike `replaceWith` but with the number of replacements included.\n\n|   | Type | Description |\n|---|------|-------------|\n| **`$regex`** | `string|string[]` |  |\n| **`$str`** | `string|string[]` |  |\n| **`$callback`** | `callable` |  |\n| **`$limit`** | `int|null` |  |\n| returns | `array` | The pair `array($replaced, $count)`. |\n| throws | `\\RuntimeException` |  |\n\n#### RegexUtil::replaceAndFilter($regex, $strs, $replacement, $limit = null)\n\nSame behavior as `replace`, except that when `$strs` is an array,\nonly strings which had a replacement performed are returned in the\nresulting array.\n\n|   | Type |\n|---|------|\n| **`$regex`** | `string|string[]` |\n| **`$strs`** | `string|string[]` |\n| **`$replacement`** | `string|string[]|callable` |\n| **`$limit`** | `int|null` |\n| returns | `string|string[]` |\n| throws | `\\RuntimeException` |\n\n#### RegexUtil::replaceAndFilterAndCount($regex, $strs, $replacement, $limit = null)\n\nLike `replaceAndFilter` but with the number of replacements included.\n\n|   | Type | Description |\n|---|------|-------------|\n| **`$regex`** | `string|string[]` |  |\n| **`$strs`** | `string|string[]` |  |\n| **`$replacement`** | `string|string[]|callable` |  |\n| **`$limit`** | `int|null` |  |\n| returns | `array` | The pair `array($replaced, $count)`. |\n| throws | `\\RuntimeException` |  |\n\n#### RegexUtil::grep($regex, $strs)\n\nTest a regular expression against an array of strings and return\nthose strings which match.\n\n|   | Type | Description |\n|---|------|-------------|\n| **`$regex`** | `string` |  |\n| **`$strs`** | `string[]` |  |\n| returns | `string[]` | The result is not re-indexed. |\n| throws | `\\RuntimeException` |  |\n\n#### RegexUtil::invertedGrep($regex, $strs)\n\nSame as `grep`, except that all of the strings which do *not* match\nthe regular expression are returned.\n\n|   | Type | Description |\n|---|------|-------------|\n| **`$regex`** | `string` |  |\n| **`$strs`** | `string[]` |  |\n| returns | `string[]` | The result is not re-indexed. |\n| throws | `\\RuntimeException` |  |\n\n#### RegexUtil::split($regex, $str, $limit = null)\n\nSplit a string by a regular expression. Optionally provide a limit\nto the number of splits.\n\n|   | Type |\n|---|------|\n| **`$regex`** | `string` |\n| **`$str`** | `string` |\n| **`$limit`** | `int|null` |\n| returns | `string[]` |\n| throws | `\\RuntimeException` |\n\n#### RegexUtil::splitWithOffsets($regex, $str, $limit = null)\n\nLike `split` but with offsets included as a second return value.\n\n|   | Type | Description |\n|---|------|-------------|\n| **`$regex`** | `string` |  |\n| **`$str`** | `string` |  |\n| **`$limit`** | `int|null` |  |\n| returns | `array` | The pair `array($parts, $offsets)`. |\n| throws | `\\RuntimeException` |  |\n\n#### RegexUtil::splitAndFilter($regex, $str, $limit = null)\n\nLike `split`, but filters out empty strings from the result.\n\n|   | Type |\n|---|------|\n| **`$regex`** | `string` |\n| **`$str`** | `string` |\n| **`$limit`** | `int|null` |\n| returns | `string[]` |\n| throws | `\\RuntimeException` |\n\n#### RegexUtil::splitAndFilterWithOffsets($regex, $str, $limit = null)\n\nLike `splitAndFilter` but with offsets included as a second return\nvalue.\n\n|   | Type | Description |\n|---|------|-------------|\n| **`$regex`** | `string` |  |\n| **`$str`** | `string` |  |\n| **`$limit`** | `int|null` |  |\n| returns | `array` | The pair `array($parts, $offsets)`. |\n| throws | `\\RuntimeException` |  |\n\n#### RegexUtil::inclusiveSplit($regex, $str, $limit = null)\n\nLike `split`, except include group 1 of the splitting pattern in the\nresults as well.\n\n|   | Type |\n|---|------|\n| **`$regex`** | `string` |\n| **`$str`** | `string` |\n| **`$limit`** | `int|null` |\n| returns | `string[]` |\n| throws | `\\RuntimeException` |\n\n#### RegexUtil::inclusiveSplitWithOffsets($regex, $str, $limit = null)\n\nLike `inclusiveSplit` but with offsets included as a second return\nvalue.\n\n|   | Type | Description |\n|---|------|-------------|\n| **`$regex`** | `string` |  |\n| **`$str`** | `string` |  |\n| **`$limit`** | `int|null` |  |\n| returns | `array` | The pair `array($parts, $offsets)`. |\n| throws | `\\RuntimeException` |  |\n\n### class Jitsu\\\\RegexUtilMatch\n\nAn object representing a regular expression match.\n\n#### new RegexUtilMatch($groups)\n\n|   | Type |\n|---|------|\n| **`$groups`** | `array` |\n\n#### $regex\\_util\\_match-\u003e\\_\\_toString()\n\n#### $regex\\_util\\_match-\u003egroups()\n\nGet the array of matched groups.\n\n|   | Type |\n|---|------|\n| returns | `array` |\n\n#### $regex\\_util\\_match-\u003egroup($i)\n\nGet a certain group.\n\n|   | Type |\n|---|------|\n| **`$i`** | `int` |\n| returns | `string` |\n\n#### $regex\\_util\\_match-\u003eoffsets()\n\nGet the array of match offsets.\n\n|   | Type | Description |\n|---|------|-------------|\n| returns | `array|null` | Null if offsets are not available. |\n\n#### $regex\\_util\\_match-\u003eoffset($i)\n\nGet the offset for a certain group.\n\n|   | Type | Description |\n|---|------|-------------|\n| **`$i`** | `int` |  |\n| returns | `int|null` | Null if the offset is not available. |\n\n### class Jitsu\\\\RegexUtilMatchWithOffsets\n\nExtends `RegexUtilMatch`.\n\nA regular expression match with match offset data.\n\n#### new RegexUtilMatchWithOffsets($groups, $offsets)\n\n#### $regex\\_util\\_match\\_with\\_offsets-\u003eoffsets()\n\n#### $regex\\_util\\_match\\_with\\_offsets-\u003eoffset($i)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbdusell%2Fjitsu-regex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbdusell%2Fjitsu-regex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbdusell%2Fjitsu-regex/lists"}