Latest 2.0
License MIT
Platforms ios 8.0, osx 10.9, watchos 2.0, tvos 9.0

CI Status

Regex.swift is yet another Regex ╬╝framework. The main difference is that it’s only 68 lines long and intends to keep things simple yet readable. It’s been used in production by millions of users and so far has proved itself 100% crash-free.

There are only 2 types: Match and Regex. If I were you I’d read the whole file, it’s really quick and will give you a better understanding.

For Swift 2.2 use version 0.1.1.
For Swift 3.x use version 1.0.3
For Swift 4.0+ use version 2.0+


Simply create a Regex by using Regex(pattern:) or Regex(pattern: options).

To verify if a String matches a Regex simply use regex.match(string:):

func validatePlate(plate: String) -> Bool {
    guard let regex = Regex(pattern: "^\w{3}-?\d{4}$") else { return false }

    return regex.match(plate)

If you want to grab the mathes use regex.matches(string:), it returns an array of Match:

public init?(plate: String) {
  guard let regex = Regex(pattern: "^(\w{3})-?(\d{4})$"),
            captureGroups = regex.matches(plate).first?.captureGroups
            where captureGroups.count == 2 else { return nil }

  letters = captureGroups[0]
  numbers = captureGroups[1]


  • Regex.swift relies on NSRegularExpression and hopefully won’t be necessary anymore when Swift gets a Swifty standard Regex API.

  • As of version 1.0 Regex.swift uses Swift 3.0.


Regex.swift is a single file with 68 lines. You can simply download it and add to your project or use CocoaPods.

Regex.swift is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "Regex.swift"


PRs and issues welcome. The only rule here is: keep it as simple as possible.


Why doesn’t Regex conform to StringLiteralConvertible?

  • Because I want its initializer to be failable.


Francesco Perrotti-Garcia, [email protected]


Regex.swift is available under the MIT license. See the LICENSE file for more info.

Latest podspec

    "name": "Regex.swift",
    "version": "2.0",
    "summary": "A Simple Swift NSRegularExpression wrapper",
    "description": "A simple wrapper for Regular Expressions using Swift 4.0",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "Francesco Perrotti-Garcia": "[email protected]"
    "source": {
        "git": "",
        "tag": "2.0"
    "social_media_url": "",
    "platforms": {
        "ios": "8.0",
        "osx": "10.9",
        "watchos": "2.0",
        "tvos": "9.0"
    "source_files": "Source/Regex.swift",
    "pushed_with_swift_version": "4.0"

Pin It on Pinterest

Share This