Skip to content

Instantly share code, notes, and snippets.

@andrevidela
Last active February 12, 2017 10:09
Show Gist options
  • Save andrevidela/8f997939ffc33bb1d8dc6f239f529495 to your computer and use it in GitHub Desktop.
Save andrevidela/8f997939ffc33bb1d8dc6f239f529495 to your computer and use it in GitHub Desktop.
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