Pure Programmer
Blue Matrix


Cluster Map

Regular Expressions

L1

This page is under construction. Please come back later.

RegEx1.swift
#!/usr/bin/env swift;
import Foundation
import Utils

// Begin Main
let s:String = "Four score and seven years ago..."
print("Match 1: " + String(s.match(NSRegularExpression(##"s.*e"##))))
print("Match 2: " + String(s.match(NSRegularExpression(##"\bs.*e\b"##))))
print("Match 3: " + String(s.match(NSRegularExpression(##"s...e"##))))
print("Match 4: " + String(s.match(NSRegularExpression(##"b.d"##))))

var subgroups:[String] = s.findFirst(NSRegularExpression(##"(\w+)\s*(\w+)"##))
print("Find First (with subgroups): " + Utils.listToString(subgroups))
subgroups = s.findFirst(NSRegularExpression(##"bad match"##))
print("Find First (bad match): " + Utils.listToString(subgroups))

var matches:[String] = s.findAll(NSRegularExpression(##"\w+"##))
print("Find All: (matches only)" + Utils.listToString(matches))
matches = s.findAll(NSRegularExpression(##"bad match"##))
print("Find All (bad match): " + Utils.listToString(matches))

exit(EXIT_SUCCESS)

Output
$ swiftc RegEx1.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)
RegEx2.swift
#!/usr/bin/env swift;
import Foundation
import Utils

// Begin Main
let s:String = "Four score and seven years ago..."
print("Replace First 1: " + s.replaceFirst(NSRegularExpression(##"\.\.\."##), "!!!"))
print("Replace First 2: " + s.replaceFirst(NSRegularExpression(##"\b...\b"##), "???"))
print("Replace First 3: " + s.replaceFirst(NSRegularExpression(##"b.d"##), "???"))
print("Replace First 4: " + s.replaceFirst(NSRegularExpression(##"(\w+) (\w+)"##), "$2 $1"))

print("Replace All 1: " + s.replaceAll(NSRegularExpression(##"\b...\b"##), "???"))
print("Replace All 2: " + s.replaceAll(NSRegularExpression(##"(\w+) (\w+)"##), "$2 $1"))

exit(EXIT_SUCCESS)

Output
$ swiftc RegEx2.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)
RegEx3.swift
#!/usr/bin/env swift;
import Foundation
import Utils

// Begin Main
let str:String = "Four score and seven years ago..."
print("Split 1: " + Utils.listToString(str.splitRegex(NSRegularExpression(##" "##))))
print("Split 2: " + Utils.listToString(str.splitRegex(NSRegularExpression(##"[eo]"##))))
print("Split 3: " + Utils.listToString(str.splitRegex(NSRegularExpression(##"\s"##))))
print("Split 4: " + Utils.listToString(str.splitRegex(NSRegularExpression(##"\W"##))))

exit(EXIT_SUCCESS)

Output
$ swiftc RegEx3.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)
swift

Questions

Projects

More ★'s indicate higher difficulty level.

References