Easily deal with Regex in Swift in a Pythonic way.

This is Easy

import PySwiftyRegex

if let m ="[Tt]his is (.*?)easy", "I think this is really easy!!!") {  // "this is really easy" // "really "

See More examples.


  • iOS 7.0+ / Mac OS X 10.9+
  • Xcode 8.0+

< For Swift 2.3 please use version 0.3.0.


Embedded frameworks require a minimum deployment target of iOS 8 or OS X Mavericks.

To use PySwiftyRegex with a project targeting iOS 7, consider using CocoaSeeds or copy the PySwiftyRegex.swift file into your project.

CocoaPods(iOS 8+, OS X 10.9+)

You can use Cocoapods to install PySwiftyRegex by adding it to your to your Podfile:

platform :ios, '8.0'

target 'MyApp' do
	pod 'PySwiftyRegex', '~> 1.0.0'

Then, run the following command:

$ pod install

Carthage(iOS 8+, OS X 10.9+)

Adding the following line to your Cartfile or Cartfile.private:

github "cezheng/PySwiftyRegex" ~> 1.0.0

Run the following command:

$ carthage update

Then drag the PySwiftyRegex.framework built by Carthage into your target's General -> Embedded Binaries.

CocoaSeeds (for iOS 7)

CocoaSeeds allows you to use Swift libraries in iOS 7 projects.

Create Seedfile:

target :MyApp do
  github 'cezheng/PySwiftyRegex', '1.0.0', :files => 'PySwiftyRegex/PySwiftyRegex.swift'

Then run the following command:

$ seed install

Now you can see the PySwiftyRegex.swift file in your Xcode project. Build and enjoy!

Supported re methods

If you are familiar with Python's re module, you are ready to go. If not, you may like to check how Python's re is better than the cumbersome NSRegularExpression's APIs, by clicking at the items below.




More Usage Examples

Compile a RegexObject for future reuse

let regex = re.compile("this(.+)that")

Matching a pattern from beginning

if let m = regex.match("this one is different from that") {  //"this one is different from that" //" one is different from "

Searching a pattern (first match)

if let m ="I want this one, not that one") {  //"this one, not that one" //" one, not "

Find all occurrences of a pattern

regex.findall("this or that, this and that") // ["this or that", "this and that"]

Get match results for all occurrences of a pattern

for m in regex.finditer("this or that, this and that") {  // 1st time: "this or that", 2nd time: "this and that" // 1st time: " or ", 2nd time: " and "

Splitting a string with pattern

let regex = re.compile("[\\+\\-\\*/]")

// By default, will split at all occurrences of the pattern
regex.split("1+2-3*4/5")    // ["1", "2", "3", "4", "5"]

// Setting a maxsplit = 2
regex.split("1+2-3*4/5", 2) // ["1", "2", "3*4/5"]

Replacing a pattern

let regex = re.compile("[Yy]ou")

// Replacing all occurrences (2 times in this example)
regex.sub("u", "You guys go grap your food")     // "u guys go grap ur food"
regex.subn("u", "You guys go grap your food")    // ("u guys go grap ur food", 2)

// Setting maximum replace count = 1 (1 times in this example)
regex.sub("u", "You guys go grap your food", 1)  // "u guys go grap your food"
regex.subn("u", "You guys go grap your food", 1) // ("u guys go grap your food", 1)


PySwiftyRegex is released under the MIT license. See LICENSE for details.