{"id":16482093,"url":"https://github.com/bod/klibregexdsl","last_synced_at":"2026-06-09T06:05:47.663Z","repository":{"id":65509652,"uuid":"295218053","full_name":"BoD/KLibRegexDsl","owner":"BoD","description":"A tiny Regex DSL library for Kotlin Multiplatform","archived":false,"fork":false,"pushed_at":"2022-10-31T17:03:02.000Z","size":402,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-11T10:39:14.964Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BoD.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-09-13T19:02:06.000Z","updated_at":"2024-11-10T14:37:42.000Z","dependencies_parsed_at":"2023-01-26T17:15:21.039Z","dependency_job_id":null,"html_url":"https://github.com/BoD/KLibRegexDsl","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BoD%2FKLibRegexDsl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BoD%2FKLibRegexDsl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BoD%2FKLibRegexDsl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BoD%2FKLibRegexDsl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BoD","download_url":"https://codeload.github.com/BoD/KLibRegexDsl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241198945,"owners_count":19926554,"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":[],"created_at":"2024-10-11T13:09:37.022Z","updated_at":"2026-06-09T06:05:47.629Z","avatar_url":"https://github.com/BoD.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# KLibRegexDsl\n\nA tiny Regex DSL library for Kotlin Multiplatform.\n\n## Why\n\n[Regular expressions](https://en.wikipedia.org/wiki/Regular_expression) are a powerful tool, but a bit of a nightmare to read and maintain.  In fact, they aren't ideal to write either: the syntax is not always intuitive or simple to remember, and it's quite easy to make mistakes - which will typically be detected at run-time only.\n\nUsing a DSL instead fixes these issues (albeit at the cost of verbosity - but as Kotlin developers, we like verbose, don't we?)\n\nAs an illustration, compare these two ways to express _\"Time Format HH:MM 12-hour, optional leading 0\"_:\n\n```regexp\n(0?[1-9]|1[0-2]]):[0-5][0-9]\n```\nvs\n```kotlin\nval hours = Group(\n    Either(\n        Sequence(\n            Characters(\"0\").onceOrNotAtAll(),\n            characterClass('1'..'9'),\n        ),\n        Sequence(\n            Characters(\"1\"),\n            characterClass('0'..'2'),\n        )\n    )\n)\nval separator = Characters(\":\")\nval minutes = Sequence(\n    characterClass('0'..'5'),\n    characterClass('0'..'9'),\n)\n\nval timeRegex = Sequence(\n    hours,\n    separator,\n    minutes\n)\n```\nWhich one is easier to read?\n\nFooled you - the first one isn't even valid (duplicated bracket)! You may have missed it, but I don't blame you: it _is_ easy to miss.\n\nIf that convinced you, continue reading.\n\n## Usage\n### 1/ Add the dependencies to your project\n\n```kotlin\ndependencies {\n    /* ... */\n    implementation(\"org.jraf:klibregexdsl:1.0.0\")\n}\n```\n\n_(The artifact is hosted on Maven Central)_\n\n\n\n### 2/ Use it\n\nThe API is pretty self-explanatory so here's an example:\n\n```kotlin\n\n// Note: this is of course a very naive implementation of an email regex\n\nval localPart = union(\n    characterClass('a'..'z'),\n    characterClass('A'..'Z'),\n    characterClass('0'..'9'),\n    characterClass('.', '+', '-'),\n).repeated(1..255)\n\nval at = Characters(\"@\")\n\nval domain = intersection(\n    union(\n        AlphanumericCharacter,\n        characterClass('.', '-'),\n    ),\n    negation(characterClass('@'))\n).oneOrMoreTimes()\n\nval tld = Group(\n    Either(\n        Characters(\".com\"),\n        Characters(\".net\"),\n        Characters(\".edu\"),\n        Characters(\".org\"),\n    )\n)\nval emailRegexNode = Sequence(\n    localPart,\n    at,\n    domain,\n    tld\n)\n\n// emailRegexNode.toString() = \"[a-zA-Z0-9.+\\\\-]{1,255}@[[\\\\p{Alnum}.\\\\-]\u0026\u0026[^@]]+(\\\\Q.com\\\\E|\\\\Q.net\\\\E|\\\\Q.edu\\\\E|\\\\Q.org\\\\E)\"\n\nval emailRegex: Regex = emailRegexNode.toRegex()\n\n```\n\nYou can also have a look at the [sample](sample/).\n\n\n## License\n\n```\nCopyright (C) 2020-present Benoit 'BoD' Lubek (BoD@JRAF.org)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbod%2Fklibregexdsl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbod%2Fklibregexdsl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbod%2Fklibregexdsl/lists"}