PySwiftyRegex - 以 Pythonic 的方式在 Swift 中轻松处理正则表达式

PySwiftyRegex

以 Pythonic 的方式在 Swift 中轻松处理正则表达式。

这很容易

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 "
}
迅速

查看更多示例

要求

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

< 对于 Swift 2.3,请使用版本 0.3.0

安装

嵌入式框架要求最低部署目标为 iOS 8 或 OS X Mavericks。

要与面向 iOS 7 的项目一起使用,请考虑使用 CocoaSeeds 或将 PySwiftyRegex.swift 文件复制到项目中。PySwiftyRegex

CocoaPods(iOS 8+, OS X 10.9+)

You can use Cocoapods to install by adding it to your to your :PySwiftyRegexPodfile

platform :ios, '8.0'
use_frameworks!

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

Then, run the following command:

$ pod install
Bash

Carthage(iOS 8+, OS X 10.9+)

Adding the following line to your or :CartfileCartfile.private

github "cezheng/PySwiftyRegex" ~> 1.0.0

Run the following command:

$ carthage update

Then drag the built by Carthage into your target's -> .PySwiftyRegex.frameworkGeneralEmbedded 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
Ruby

Then run the following command:

$ seed install
Console

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

More Usage Examples

Compile a RegexObject for future reuse

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

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 "
}
Swift

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 "
}
Swift

Find all occurrences of a pattern

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

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 "
}
Swift

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"]
Swift

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)
Swift

License

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

GitHub

https://github.com/cezheng/PySwiftyRegex