Latest 4.1
Homepage https://github.com/johnno1962/SwiftRegex4
License MIT
Platforms osx 10.11
Authors

SwiftRegex4 – basic regex operations in Swift4

A basic regular expression library based on the idea that subscripting into a string with
a string should be a regexp match.

//: Regexies represented as a String subscript on a String

var input = "Now is the time for all good men to come to the aid of the party"

// basic regex match
if input["\w+"] {
    print("match")
}

// receiving type controls data you get
if let firstMatch: Substring = input["\w+"] {
    print("match: (firstMatch)")
}

if let groupsOfFirstMatch: [Substring?] = input["(all) (\w+)"] {
    print("groups: (groupsOfFirstMatch)")
}

if let allGroupsOfAllMatches: [[Substring?]] = input["(\w)(\w*)"] {
    print("allGroups: (allGroupsOfAllMatches)")
}

// Matches only can be fetched with non-optional Substring array
if let allMatches: [Substring] = input["(\w)\w+"] {
    print("words: (allMatches)")
}

// regex replace by assignment
input["men"] = "folk"
print(input)

// replace just the first two matches
input["\w+"] = ["yesterday", "was"]
print(input)

// individual groups can be accessed
input["(all) (\w+)", 2]

// and assigned to
input["the (\w+)", [.caseInsensitive], 1] = "_$1_"
print(input)

// or replaced using a closure
input["(_?)(\w)(\w*)"] = {
    (groups, stop) in
    return groups[1]!+groups[2]!.uppercased()+groups[3]!
}
print(input)

// parsing a properties file
let props = """
    name1 = value1
    name2 = value2
    """

var params = [String: String]()
for groups in props["(\w+)\s*=\s*(.*)"] {
    params[String(groups[1]!)] = String(groups[2]!)
}
print(params)

Latest podspec

{
    "name": "SwiftRegex4",
    "version": "4.1",
    "summary": "Regular expressions for Swift 4",
    "homepage": "https://github.com/johnno1962/SwiftRegex4",
    "social_media_url": "https://twitter.com/Injection4Xcode",
    "documentation_url": "https://github.com/johnno1962/SwiftRegex4/blob/master/README.md",
    "license": {
        "type": "MIT"
    },
    "authors": {
        "johnno1962": "[email protected]"
    },
    "platforms": {
        "osx": "10.11"
    },
    "source": {
        "git": "https://github.com/johnno1962/SwiftRegex4.git",
        "tag": "4.1"
    },
    "source_files": "SwiftRegex4.playground/Sources/*.swift",
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This