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

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
Match 1: true Match 2: true Match 3: true Match 4: false Find First (with subgroups): ["Four score", "Four", "score"] Find First (bad match): [] Find All: (matches only)["Four", "score", "and", "seven", "years", "ago"] Find All (bad match): []
RegEx2.swift
#!/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
Replace First 1: Four score and seven years ago!!! Replace First 2: Four score ??? seven years ago... Replace First 3: Four score and seven years ago... Replace First 4: score Four and seven years ago... Replace All 1: Four score ??? seven years ???... Replace All 2: score Four seven and ago years...
RegEx3.swift
#!/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
Split 1: ["Four", "score", "and", "seven", "years", "ago..."] Split 2: ["F", "ur sc", "r", " and s", "v", "n y", "ars ag", "..."] Split 3: ["Four", "score", "and", "seven", "years", "ago..."] Split 4: ["Four", "score", "and", "seven", "years", "ago", "", ""]
swift

Questions

Projects

More ★'s indicate higher difficulty level.

References