Awesome
PySwiftyRegex
Easily deal with Regex in Swift in a Pythonic way.
This is Easy
import PySwiftyRegex
if let m = re.search("[Tt]his is (.*?)easy", "I think this is really easy!!!") {
m.group() // "this is really easy"
m.group(1) // "really "
}
See More examples.
Requirements
- iOS 7.0+ / Mac OS X 10.9+
- Xcode 8.0+
< For Swift 2.3 please use version 0.3.0.
Installation
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'
use_frameworks!
target 'MyApp' do
pod 'PySwiftyRegex', '~> 1.0.0'
end
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'
end
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.
re
re.RegexObject
re.MatchObject
<a name="more_usage"></a>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") {
m.group() //"this one is different from that"
m.group(1) //" one is different from "
}
Searching a pattern (first match)
if let m = regex.search("I want this one, not that one") {
m.group() //"this one, not that one"
m.group(1) //" 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") {
m.group() // 1st time: "this or that", 2nd time: "this and that"
m.group(1) // 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)
License
PySwiftyRegex
is released under the MIT license. See LICENSE for details.