Last active
February 12, 2017 10:09
-
-
Save andrevidela/8f997939ffc33bb1d8dc6f239f529495 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func parse(source: String) -> Person? { | |
let lines = source.components(separatedBy: "\n") | |
let age = matches(for: "(?<=age: )[0-9]+", in: lines[1]).flatMap({Int($0)}) | |
let name = matches(for: "(?<=name: )\\w+", in: lines[2]) | |
let city = matches(for: "(?<=city: )[\\w ]+", in: lines[3]) | |
if let a = age, let n = name, let c = city { | |
return Person(name: n, age: a, city: c) | |
} else { | |
return nil | |
} | |
} | |
let parsedPerson = parse(source: source) | |
print(parsedPerson) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment