Latest 1.0.2
License BSD 3-Clause
Platforms ios 11.0

Swift String utilities


AppDeveloperKit-String is a Swift String library that provides support for subscripting and easy to use regular expression matching and substitutions.

  • [x] Perl inspired matches and substitutions
    • [x] Implementation as String Extension
    • [x] =~ operator support
    • [x] Optional alternative interface using Regex wrapper class
    • [x] Unicode characters supported including ZWJ sequences to combine elements 👩‍🚀
  • [x] Flexible matching
    • [x] Support for global and case-insensitive flags
    • [x] Template support
    • [x] Match output as capture groups or Bool result
    • [x] Capture groups can be returned as tuple or array
    • [x] Prematch, match and postmatch provided in closure.
  • [x] Subscripting by index, Range and ClosedRange
  • [x] Comprehensive unit tests for interfaces

Installation via Cocoapods

Add one of the following options to your Podfile:

Recommended: Install all the components of AppDeveloperKit-String

pod 'AppDeveloperKit-String'

Minimal installation
In the event of a module conflict with the Regex wrapper class, you can choose to only add matching and substitution support for =~ operator, along with subscripting support:

pod 'AppDeveloperKit-String/String'
pod 'AppDeveloperKit-String/Subscript'

Integrate your dependencies using frameworks by adding use_frameworks! to your Podfile. Then run pod install.


See Documentation.playground for a complete specification of AppDeveloperKit-String presented as a Swift playground.

There is also a PDF version available, though the export process from the playground does result in a somewhat large font size.

Match Examples

Capture groups returned as a tuple. Closure provides prematch, match and postmatch.

var (d1,d2) = "A56B" =~ (.M,"(\d)(\d)", { (preMatch, match, postMatch) in 
    preMatch // A
    match    // 56
    postMatch // B
 // ("5","6")

If you only need the capture groups, you can simplify to:

var (d1,d2) = "A56B" =~ (.M,"(\d)(\d)")

Same capture groups returned as array:

var arr: [String] = "A56B" =~ (.M,"(\d)(\d)")  

Array can be an Optional if you wish (nil indicates no match):

var arr: [String]? = "A56B" =~ (.M,"(\d)(\d)")  

Tuple or array result, with or without a closure, also supports global "g" and/or case-insensitive "i" flags. Flags argument comes after the pattern (and before optional closure).

var arr: [String] = "4XY5" =~ (.M,"([a-z])", "gi", { (preMatch, match, postMatch) in   
    preMatch // 4
    match    // Y
    postMatch // 5
//  ["X","Y"]

An alternate interface using the Regex class (also available for tuple, Bool result)

var arr: [String]  = Regex.m(str: "4XY5", pattern: "([a-z])", flags: "ig", completion: { (preMatch, match, postMatch) in  

Bool result indicates a match

var result1: Bool = "XY" =~ (.M,"\d") // false

// With flags argument
var result2: Bool = "XY" =~ (.M,"[a-z]", "i")  // true

Substitution Examples

In-place substitution using a pattern and replacement with template. Returns count of subsitutions made.

var str = "XY"
let count = str =~ (.S,"(\w)(\w)","$2$1") // 1   str = YX

// With flags argument
str = "XY ZW"
let count = str =~ (.S, "(\w)(\w)","$2$1", "g") // 2  str = YX WZ

An alternate interface using the Regex class

str = "XY"
let count = Regex.s(str: &str, pattern: "([a-z])([a-z])", replacement: "$2$1", flags: "i") // 1  str = YX

Subscripting Examples

Subscripting by index, Range and ClosedRange. Unicode supported.

str = "A😎B👩‍🚀CDE"

str[1] // 😎
str[2] // B
str[3] // 👩‍🚀

str[1..<4] // 😎B👩‍🚀

str[1...4] // 😎B👩‍🚀C

Latest podspec

    "name": "AppDeveloperKit-String",
    "version": "1.0.2",
    "summary": "String utilities - regular expressions and subscripting",
    "homepage": "",
    "authors": {
        "Name": "Scott Carter"
    "license": {
        "type": "BSD 3-Clause",
        "file": "LICENSE"
    "swift_version": "4.2",
    "platforms": {
        "ios": "11.0"
    "source": {
        "git": "",
        "tag": "1.0.2"
    "subspecs": [
            "name": "String",
            "source_files": "Library/String.swift"
            "name": "Regex",
            "source_files": "Library/Regex.swift",
            "dependencies": {
                "AppDeveloperKit-String/String": []
            "name": "Subscript",
            "source_files": "Library/Subscript.swift"

Pin It on Pinterest

Share This