Regular Expressions

This page is under construction. Please come back later.
#!/usr/bin/env swift;
import Foundation
import Utils
func main() -> Void {
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)
}
main()
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)
#!/usr/bin/env swift;
import Foundation
import Utils
func main() -> Void {
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)
}
main()
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)
#!/usr/bin/env swift;
import Foundation
import Utils
func main() -> Void {
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)
}
main()
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)
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[Swift Community]]
- [[Swift Language Guide]]
- [[Swift Language Reference]]
- [[Swift Programming Language]], Apple Inc.
- [[Swift Doc]]
- [[We Heart Swift]]
- [[Swift Cookbook]]
- [[Swift Playground]]
- [[Swift at TutorialsPoint]]
- [[Hacking with Swift]]
Pure Programmer


