{"id":1856,"url":"https://github.com/sharplet/Regex","last_synced_at":"2025-08-02T05:32:52.328Z","repository":{"id":52929928,"uuid":"45213675","full_name":"sharplet/Regex","owner":"sharplet","description":"A delightful and expressive regular expression type for Swift.","archived":false,"fork":false,"pushed_at":"2021-04-13T14:42:19.000Z","size":8650,"stargazers_count":612,"open_issues_count":6,"forks_count":48,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-11-29T06:54:03.976Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","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/sharplet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-29T22:09:51.000Z","updated_at":"2024-06-07T10:26:30.000Z","dependencies_parsed_at":"2022-08-26T01:40:36.300Z","dependency_job_id":null,"html_url":"https://github.com/sharplet/Regex","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sharplet%2FRegex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sharplet%2FRegex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sharplet%2FRegex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sharplet%2FRegex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sharplet","download_url":"https://codeload.github.com/sharplet/Regex/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228443619,"owners_count":17920760,"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-01-05T20:15:57.408Z","updated_at":"2024-12-06T09:30:26.074Z","avatar_url":"https://github.com/sharplet.png","language":"Swift","funding_links":[],"categories":["Regex","Swift"],"sub_categories":["React-Like","Other free courses"],"readme":"# Regex\n\nPattern match like a boss.\n\n\n\n## Usage\n\nCreate:\n\n```swift\n// Use `Regex.init(_:)` to build a regex from a static pattern\n\nlet greeting = Regex(\"hello (world|universe)\")\n\n// Use `Regex.init(string:)` to construct a regex from dynamic data, and\n// gracefully handle invalid input\n\nvar validations: [String: Regex]\n\nfor (name, pattern) in config.loadValidations() {\n  do {\n    validations[name] = try Regex(string: pattern)\n  } catch {\n    print(\"error building validation \\(name): \\(error)\")\n  }\n}\n```\n\nMatch:\n\n```swift\nif greeting.matches(\"hello universe!\") {\n  print(\"wow, you're friendly!\")\n}\n```\n\n_Pattern_ match:\n\n```swift\nswitch someTextFromTheInternet {\ncase Regex(\"DROP DATABASE (.+)\"):\n  // TODO: patch security hole\ndefault:\n  break\n}\n```\n\nCapture:\n\n```swift\nlet greeting = Regex(\"hello (world|universe|swift)\")\n\nif let subject = greeting.firstMatch(in: \"hello swift\")?.captures[0] {\n  print(\"ohai \\(subject)\")\n}\n```\n\nFind and replace:\n\n```swift\n\"hello world\".replacingFirst(matching: \"h(ello) (\\\\w+)\", with: \"H$1, $2!\")\n// \"Hello, world!\"\n```\n\nAccessing the last match:\n\n```swift\nswitch text {\ncase Regex(\"hello (\\\\w+)\"):\n  if let friend = Regex.lastMatch?.captures[0] {\n    print(\"lovely to meet you, \\(friend)!\")\n  }\ncase Regex(\"goodbye (\\\\w+)\"):\n  if let traitor = Regex.lastMatch?.captures[0] {\n    print(\"so sorry to see you go, \\(traitor)!\")\n  }\ndefault:\n  break\n}\n```\n\nOptions:\n\n```swift\nlet totallyUniqueExamples = Regex(\"^(hello|foo).*$\", options: [.ignoreCase, .anchorsMatchLines])\nlet multilineText = \"hello world\\ngoodbye world\\nFOOBAR\\n\"\nlet matchingLines = totallyUniqueExamples.allMatches(in: multilineText).map { $0.matchedString }\n// [\"hello world\", \"FOOBAR\"]\n```\n\nDecode:\n\n```swift\nlet json = \"\"\"\n  [\n    {\n      \"name\" : \"greeting\",\n      \"pattern\" : \"^(\\\\\\\\w+) world!$\"\n    }\n  ]\n  \"\"\".data(using: .utf8)!\n\nstruct Validation: Codable {\n  var name: String\n  var pattern: Regex\n}\n\nlet decoder = JSONDecoder()\ntry decoder.decode(Validation.self, from: json)\n// Validation(name: \"greeting\", pattern: /^(\\w+) world!/)\n```\n\nRanges:\n\n```swift\nlet lyrics = \"\"\"\n  So it's gonna be forever\n  Or it's gonna go down in flames\n  \"\"\"\n\nlet possibleEndings = Regex(\"it's gonna (.+)\")\n    .allMatches(in: lyrics)\n    .flatMap { $0.captureRanges[0] }\n    .map { lyrics[$0] }\n\n// it's gonna: [\"be forever\", \"go down in flames\"]\n```\n\n\n\n## Installation\n\nRegex supports Swift 4.2 and above, on all Swift platforms.\n\n#### Swift Package Manager\n\nAdd a dependency to your `Package.swift`:\n\n```swift\nlet package = Package(\n  name: \"MyPackage\",\n  dependencies: [\n    // other dependencies...\n    .package(url: \"https://github.com/sharplet/Regex.git\", from: \"2.1.0\"),\n  ]\n)\n```\n\n#### Carthage\n\nPut this in your Cartfile:\n\n```\ngithub \"sharplet/Regex\" ~\u003e 2.1\n```\n\n#### CocoaPods\n\nPut this in your Podfile:\n\n```ruby\npod \"STRegex\", \"~\u003e 2.1\"\n```\n\n\n\n## Contributing\n\nSee [CONTRIBUTING.md](CONTRIBUTING.md).\n\n\n\n## Development Setup\n\n### Swift Package Manager\n\nBuild and run the tests:\n\n```\nswift test\n\n# or just\n\nrake test:package\n```\n\nIf you're on a Mac, testing on Linux is supported via [Docker for Mac](https://www.docker.com/docker-mac).\nOnce Docker is set up, start a Linux shell:\n\n```\nrake docker\n```\n\nAnd run the tests via Swift Package Manager.\n\n### Xcode\n\n`xcpretty` is recommended, for prettifying test output:\n\n```\ngem install xcpretty\n```\n\nThen run the tests:\n\n```\n# one of\nrake test:osx\nrake test:ios\nrake test:tvos\n```\n\n### Formatting \u0026 Linting\n\nRegex uses [SwiftFormat](https://github.com/nicklockwood/SwiftFormat) to\nmaintain consistent code formatting.\n\nRegex uses [SwiftLint](https://github.com/realm/SwiftLint) to validate code style.\nSwiftLint is automatically run against pull requests using [Hound CI](https://houndci.com/).\n\nWhen submitting a pull request, running these tools and addressing any issues\nis much appreciated!\n\n```\nbrew bundle\nswiftformat .\nswiftlint\n```\n\n\n\n## License\n\nSee [LICENSE.txt](LICENSE.txt).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsharplet%2FRegex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsharplet%2FRegex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsharplet%2FRegex/lists"}