{"id":14985383,"url":"https://github.com/simpleregex/srl-php","last_synced_at":"2025-05-14T22:08:11.868Z","repository":{"id":56456276,"uuid":"65804057","full_name":"SimpleRegex/SRL-PHP","owner":"SimpleRegex","description":"Simple Regex Language","archived":false,"fork":false,"pushed_at":"2022-09-02T10:26:59.000Z","size":106,"stargazers_count":1797,"open_issues_count":13,"forks_count":112,"subscribers_count":51,"default_branch":"master","last_synced_at":"2025-05-14T00:42:31.606Z","etag":null,"topics":["composer","composer-packages","query-builder","regular-expression","srl"],"latest_commit_sha":null,"homepage":"https://simple-regex.com","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/SimpleRegex.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-16T08:56:01.000Z","updated_at":"2024-12-10T13:09:54.000Z","dependencies_parsed_at":"2022-08-15T19:10:15.499Z","dependency_job_id":null,"html_url":"https://github.com/SimpleRegex/SRL-PHP","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimpleRegex%2FSRL-PHP","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimpleRegex%2FSRL-PHP/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimpleRegex%2FSRL-PHP/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SimpleRegex%2FSRL-PHP/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SimpleRegex","download_url":"https://codeload.github.com/SimpleRegex/SRL-PHP/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254235698,"owners_count":22036963,"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":["composer","composer-packages","query-builder","regular-expression","srl"],"created_at":"2024-09-24T14:10:50.533Z","updated_at":"2025-05-14T22:08:06.859Z","avatar_url":"https://github.com/SimpleRegex.png","language":"PHP","readme":"# Simple Regex Language\n\n[![codecov](https://codecov.io/gh/SimpleRegex/SRL-PHP/branch/master/graph/badge.svg)](https://codecov.io/gh/SimpleRegex/SRL-PHP)\n[![Build Status](https://travis-ci.org/SimpleRegex/SRL-PHP.svg?branch=master)](https://travis-ci.org/SimpleRegex/SRL-PHP)\n[![License](https://poser.pugx.org/simpleregex/srl-php/license)](https://packagist.org/packages/simpleregex/srl-php)\n\nWe all know Regular Expressions are hard to read. Once written you're\nhappy if you never ever have to touch this line of code again because\nyou're going to have a hard time understanding what you've written there.\n\nBefore we get started, a short note on how to use SRL: You can either\nuse this project directly, or, if you're not into PHP or including a\nlibrary like that, you can build your query online and use the generated\nRegular Expression elsewhere:\n\nhttps://simple-regex.com/build\n\n## An Example\n\nRegular Expressions don't have to be bulky? - No, they don't!\nJust have a look at this:\n\n```\nbegin with any of (digit, letter, one of \"._%+-\") once or more,\nliterally \"@\",\nany of (digit, letter, one of \".-\") once or more,\nliterally \".\",\nletter at least 2,\nmust end, case insensitive\n```\n\nOr, if you like, a implementation in code itself:\n\n```php\n$query = SRL::startsWith()\n    -\u003eanyOf(function (Builder $query) {\n        $query-\u003edigit()\n            -\u003eletter()\n            -\u003eoneOf('._%+-');\n    })-\u003eonceOrMore()\n    -\u003eliterally('@')\n    -\u003eanyOf(function (Builder $query) {\n        $query-\u003edigit()\n            -\u003eletter()\n            -\u003eoneOf('.-');\n    })-\u003eonceOrMore()\n    -\u003eliterally('.')\n    -\u003eletter()-\u003eatLeast(2)\n    -\u003emustEnd()-\u003ecaseInsensitive();\n```\n\nYes, indeed, both examples are definitely longer than the corresponding\nregular expression:\n\n```\n/^([A-Z0-9._%+-])+@[A-Z0-9.-]+\\.[A-Z]{2,}$/i\n```\n\nBut, however, the above is quite better to read and definitely better\nto maintain, isn't it? And to top that off: It's much harder to forget\nto escape something like a dot in SRL.\n\nLet's go through it real quick:\n\n1. First, we require the matching string to start. This way, we make sure\nthe match won't begin in the middle of something.\n2. Now, we're matching either a digit, a letter, or one of the literal\ncharacters ., _, %, + or -. We expect there to be one or more of them.\n3. We now expect exactly one @ - Looks like an email address.\n4. Again, either digits, letters or . or -, once or multiple times.\n5. A dot. Seems to be the end of the TLDs name\n6. To the end, we'll expect two or more letters, for the TLD.\n7. We require the string to end now, to avoid matching stuff like \n`invalid@email.com123`.\n8. And of course, all of that should be case insensitive, since it's\nan email address.\n\n## Features\n\n### Using the Language\n\nAbove you can see two examples. The first one uses the language itself,\nthe second one the Query Builder. Since using a language is more fluent\nthan a builder, we wanted to make things as easy as possible for you.\n\n```php\n$srl = new SRL('literally \"colo\", optional \"u\", literally \"r\"');\npreg_match($srl, 'color') // 1\n$srl-\u003eisMatching('colour') // true\n$srl-\u003eisMatching('soup') // false\n```\n\nEverything below applies to both, the SRL itself and the Query Builder.\n\n### Matching\n\nSRL is as simple as the example above states. To retrieve\nthe built Regular Expression which can be used by external tools like\n[preg_match](http://php.net/manual/en/function.preg-match.php), either\nuse the `-\u003eget()` method, or just let it cast to a string:\n\n```php\npreg_match($query, 'sample@email.com');\n```\n\nOf course, you may use the builtin match methods for an even easier\napproach:\n\n```php\n$query-\u003eisMatching('sample@email.com'); // true\n$query-\u003eisMatching('invalid-email.com'); // false\n```\n\n### Capture Groups\n\nSince regular expressions aren't only used for validation, capturing\ngroups is supported by SRL as well. After defining the Regular\nExpression just like before, simply add a `capture`-group which will\nmatch the query defined in the lambda function. Optionally, a name for\nthat capture group (`color`) can be set as well:\n\n```php\n// Using SRL\n$regEx = new SRL('literally \"color:\", whitespace, capture (letter once or more) as \"color\", literally \".\"');\n\n// Using the query builder\n$regEx = SRL::literally('color:')-\u003ewhitespace()-\u003ecapture(function (Builder $query) {\n    $query-\u003eletter()-\u003eonceOrMore();\n}, 'color')-\u003eliterally('.');\n\n$matches = $regEx-\u003egetMatches('Favorite color: green. Another color: yellow.');\n\necho $matches[0]-\u003eget('color'); // green\necho $matches[1]-\u003eget('color'); // yellow\n```\n\nEach match will be passed to a `SRL\\Match` object, which will return the\nmatches found.\n\n### Additional PCRE functions\n\nFeel free to use all the available [PCRE PHP functions](http://php.net/manual/en/ref.pcre.php)\nin combination with SRL. Although, why bother? We've got wrappers for\nall common functions with additional features. Just like above, just\napply one of the following methods directly on the SRL or Builder:\n\n* `isMatching()` - Validate if the expression matches the given string.\n* `getMatches()` - Get all matches for supplied capture groups.\n* `getMatch()` - Get first match for supplied capture groups.\n* `replace()` - Replace data using the expression.\n* `split()` - Split string into array through expression.\n* `filter()` - Filter items using the expression.\n\n### Lookarounds\n\nIn case you want some regular expressions to only apply in certain\nconditions, lookarounds are probably what you're searching for.\n\nWith queries like:\n\n```php\n// SRL:\nnew SRL('capture (literally \"foo\") if followed by (literally \"bar\")');\n\n// Query Builder:\nSRL::capture(function (Builder $query) {\n    $query-\u003eliterally('foo');\n})-\u003eifFollowedBy(function (Builder $query) {\n    $query-\u003eliterally('bar');\n});\n```\n\nyou can easily capture 'foo', but only if this match is followed by\n'bar'.\n\nBut to be honest, the Query Builder version is quite much code for\nsuch a simple thing, right? No problem! Not only are we supporting\nanonymous functions for sub-expressions, strings and Builder objects\nare supported as well.\nIsn't that great? Just have a look at one possible example:\n\n```php\nSRL::capture('foo')-\u003eifFollowedBy(SRL::literally('bar'));\n```\n\nIf desired, lookbehinds are possible as well. Using `ifAlreadyHad()`\nyou can validate a certain condition only if the previous string\ncontained a specific pattern.\n\n## Performance\n\nThe built Regular Expression will be cached, so you don't have to worry\nabout it being created every time you call the `match`-method. And,\nsince it's a normal Regular Expression under the hood, performance\nwon't be an issue.\n\nOf course, building the expression may take some time, but in real life\napplications this shouldn't be noticeable. But if you like, you can\nbuild the expression somewhere else and just use the result in your app.\nIf you do that, please keep the code for that query somewhere and link\nto it, otherwise the Regular Expression will be unreadable just as before.\n\n## Usage\n\nAdd the package to your ``require`` section in the ``composer.json``-file\nand update your project.\n\n```json\n\"require\": {\n    \"simpleregex/srl-php\": \"0.1.x-dev\"\n}\n```\n\n```sh\ncomposer update\n```\n\n## Things to do\n\nWe're definitely not done yet. There's much to come. A short list of\nstuff that's planned would contain:\n\n* More functionality\n* More documentation\n* Variable support\n* Rule the world\n\n## License\n\nSRL is published under the MIT license. See `LICENSE` for more information.\n\n## Contribution\n\nLike this project? Want to contribute? Awesome! Feel free to open some\npull requests or just open an issue.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimpleregex%2Fsrl-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimpleregex%2Fsrl-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimpleregex%2Fsrl-php/lists"}