{"id":1859,"url":"https://github.com/crossroadlabs/Regex","last_synced_at":"2025-08-06T14:33:00.270Z","repository":{"id":56906826,"uuid":"50254222","full_name":"crossroadlabs/Regex","owner":"crossroadlabs","description":"Regular expressions for swift","archived":false,"fork":false,"pushed_at":"2019-04-07T14:22:50.000Z","size":382,"stargazers_count":332,"open_issues_count":7,"forks_count":33,"subscribers_count":15,"default_branch":"master","last_synced_at":"2024-11-21T21:48:02.280Z","etag":null,"topics":["crossroad-labs","regex","regular-expression","split","swift"],"latest_commit_sha":null,"homepage":null,"language":"Swift","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/crossroadlabs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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-01-23T19:21:42.000Z","updated_at":"2024-11-19T20:08:45.000Z","dependencies_parsed_at":"2022-08-21T03:20:48.098Z","dependency_job_id":null,"html_url":"https://github.com/crossroadlabs/Regex","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crossroadlabs%2FRegex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crossroadlabs%2FRegex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crossroadlabs%2FRegex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crossroadlabs%2FRegex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crossroadlabs","download_url":"https://codeload.github.com/crossroadlabs/Regex/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228915455,"owners_count":17991409,"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":["crossroad-labs","regex","regular-expression","split","swift"],"created_at":"2024-01-05T20:15:57.473Z","updated_at":"2024-12-09T15:30:49.286Z","avatar_url":"https://github.com/crossroadlabs.png","language":"Swift","readme":"[![by Crossroad Labs](./header.png)](http://www.crossroadlabs.xyz/)\n\n# Regex\n\n![🐧 linux: ready](https://img.shields.io/badge/%F0%9F%90%A7%20linux-ready-red.svg)\n[![GitHub license](https://img.shields.io/badge/license-Apache%202.0-lightgrey.svg)](https://raw.githubusercontent.com/crossroadlabs/Regex/master/LICENSE)\n[![Build Status](https://travis-ci.org/crossroadlabs/Regex.svg?branch=master)](https://travis-ci.org/crossroadlabs/Regex)\n[![GitHub release](https://img.shields.io/github/release/crossroadlabs/Regex.svg)](https://github.com/crossroadlabs/Regex/releases)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![CocoaPods version](https://img.shields.io/cocoapods/v/CrossroadRegex.svg)](https://cocoapods.org/pods/CrossroadRegex)\n![Platform OS X | iOS | tvOS | watchOS | Linux](https://img.shields.io/badge/platform-Linux%20%7C%20OS%20X%20%7C%20iOS%20%7C%20tvOS%20%7C%20watchOS-orange.svg)\n\n## Advanced regular expressions for Swift\n\n## Goals\n\n[\u003cimg align=\"left\" src=\"https://raw.githubusercontent.com/crossroadlabs/Express/master/logo-full.png\" hspace=\"20\" height=128\u003e](https://github.com/crossroadlabs/Express) Regex library was mainly introduced to fulfill the needs of [Swift Express](https://github.com/crossroadlabs/Express) - web application server side framework for Swift.\n\nStill we hope it will be useful for everybody else.\n\n[Happy regexing ;)](#examples)\n\n## Features\n\n- [x] Deep Integration with Swift\n\t- [x] `=~` operator support\n\t- [x] Swift Pattern Matching (aka `switch` operator) support\n- [x] Named groups\n- [x] Match checking\n- [x] Extraction/Search functions\n- [x] Replace functions\n\t- [x] With a pattern\n\t- [x] With custom replacing function\n- [x] Splitting with a Regular Expression\n\t- [x] Simple\n\t- [x] With groups\n- [x] String extensions\n- [x] Supports grapheme clusters 👨‍👩‍👧\n\n## Extra\n\nPath to Regex converter is available as a separate library here: [PathToRegex](https://github.com/crossroadlabs/PathToRegex)\n\nThis one allows using path patterns like `/folder/*/:file.txt` or `/route/:one/:two` to be converted to Regular Expressions and matched against strings.\n\n## Getting started\n\n### Installation\n\n#### [Package Manager](https://swift.org/package-manager/)\n\nAdd the following dependency to your [Package.swift](https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md#define-dependencies):\n\n```swift\n.Package(url: \"https://github.com/crossroadlabs/Regex.git\", majorVersion: 1)\n```\n\nRun ```swift build``` and build your app.\n\n#### [CocoaPods](http://cocoapods.org/)\nAdd the following to your [Podfile](http://guides.cocoapods.org/using/the-podfile.html):\n\n```rb\npod 'CrossroadRegex'\n```\n\nMake sure that you are integrating your dependencies using frameworks: add `use_frameworks!` to your Podfile. Then run `pod install`.\n\n#### [Carthage](https://github.com/Carthage/Carthage)\nAdd the following to your [Cartfile](https://github.com/Carthage/Carthage/blob/master/Documentation/Artifacts.md#cartfile):\n\n```\ngithub \"crossroadlabs/Regex\"\n```\n\nRun `carthage update` and follow the steps as described in Carthage's [README](https://github.com/Carthage/Carthage#adding-frameworks-to-an-application).\n\n#### Manually\n1. Download and drop ```/Regex``` folder in your project.  \n2. Congratulations! \n\n### Examples\n\n#### Hello Regex:\n\nAll the lines below are identical and represent simple matching. All operators and `matches` function return Bool\n\n```swift\n//operator way, can match either regex or string containing pattern\n\"l321321alala\" =~ \"(.+?)([123]*)(.*)\".r\n\"l321321alala\" =~ \"(.+?)([123]*)(.*)\"\n\n//similar function\n\"(.+?)([123]*)(.*)\".r!.matches(\"l321321alala\")\n```\nOperator `!~` returns `true` if expression does **NOT** match:\n\n```swift\n\"l321321alala\" !~ \"(.+?)([123]*)(.*)\".r\n\"l321321alala\" !~ \"(.+?)([123]*)(.*)\"\n//both return false\n```\n\n#### Swift Pattern Matching (aka `switch` keyword)\n\nRegex provides very deep integration with Swift and can be used with the switch keyword in the following way:\n\n```swift\nlet letter = \"a\"\nlet digit = \"1\"\nlet other = \"!\"\n\n//you just put your string is a regular Swift's switch to match to regular expressions\nswitch letter {\n\t//note .r after the string literal of the pattern\n\tcase \"\\\\d\".r: print(\"digit\")\n\tcase \"[a-z]\".r: print(\"letter\")\n\tdefault: print(\"bizarre symbol\")\n}\n\nswitch digit {\n\tcase \"\\\\d\".r: print(\"digit\")\n\tcase \"[a-z]\".r: print(\"letter\")\n\tdefault: print(\"bizarre symbol\")\n}\n\nswitch other {\n\t//note .r after the string literal of the pattern\n\tcase \"\\\\d\".r: print(\"digit\")\n\tcase \"[a-z]\".r: print(\"letter\")\n\tdefault: print(\"bizarre symbol\")\n}\n```\n\nThe output of the code above will be:\n\n```\nletter\ndigit\nbizarre symbol\n```\n\n#### Accessing groups:\n\n```swift\n// strings can be converted to regex in Scala style .r property of a string\nlet digits = \"(.+?)([123]*)(.*)\".r?.findFirst(in: \"l321321alala\")?.group(at: 2)\n// digits is \"321321\" here\n```\n\n#### Named groups:\n\n```swift\nlet regex:RegexType = try Regex(pattern:\"(.+?)([123]*)(.*)\",\n                                        groupNames:\"letter\", \"digits\", \"rest\")\nlet match = regex.findFirst(in: \"l321321alala\")\nif let match = match {\n\tlet letter = match.group(named: \"letter\")\n\tlet digits = match.group(named: \"digits\")\n\tlet rest = match.group(named: \"rest\")\n\t//do something with extracted data\n}\n```\n\n#### Replace:\n\n```swift\nlet replaced = \"(.+?)([123]*)(.*)\".r?.replaceAll(in: \"l321321alala\", with: \"$1-$2-$3\")\n//replaced is \"l-321321-alala\"\n```\n\n#### Replace with custom replacer function:\n\n```swift\nlet replaced = \"(.+?)([123]+)(.+?)\".r?.replaceAll(in: \"l321321la321a\") { match in\n\tif match.group(at: 1) == \"l\" {\n\t\treturn nil\n\t} else {\n\t\treturn match.matched.uppercaseString\n\t}\n}\n//replaced is \"l321321lA321A\"\n```\n\n#### Split:\n\nIn the following example, split() looks for 0 or more spaces followed by a semicolon followed by 0 or more spaces and, when found, removes the spaces from the string. nameList is the array returned as a result of split().\n\n```swift\nlet names = \"Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand\"\nlet nameList = names.split(using: \"\\\\s*;\\\\s*\".r)\n//name list contains [\"Harry Trump\", \"Fred Barney\", \"Helen Rigby\", \"Bill Abel\", \"Chris Hand\"]\n```\n\n#### Split with groups:\n\nIf separator contains capturing parentheses, matched results are returned in the array.\n\n```swift\nlet myString = \"Hello 1 word. Sentence number 2.\"\nlet splits = myString.split(using: \"(\\\\d)\".r)\n//splits contains [\"Hello \", \"1\", \" word. Sentence number \", \"2\", \".\"]\n```\n\n## Changelog\n\nYou can view the [CHANGELOG](./CHANGELOG.md) as a separate document [here](./CHANGELOG.md).\n\n## Contributing\n\nTo get started, \u003ca href=\"https://www.clahub.com/agreements/crossroadlabs/Regex\"\u003esign the Contributor License Agreement\u003c/a\u003e.\n\n## [![Crossroad Labs](http://i.imgur.com/iRlxgOL.png?1) by Crossroad Labs](http://www.crossroadlabs.xyz/)\n","funding_links":[],"categories":["Regex","Libs","Swift","Text [🔝](#readme)"],"sub_categories":["React-Like","Text","Other free courses"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrossroadlabs%2FRegex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrossroadlabs%2FRegex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrossroadlabs%2FRegex/lists"}