I tried to translate a PHP function to Swift. The function is used to format a String to an other according the my regular expression. So here's what I do in PHP :
preg_match('/P(([0-9]+)Y)?(([0-9]+)M)?(([0-9]+)D)?T?(([0-9]+)H)?(([0-9]+)M)?(([0-9]+)(\.[0-9]+)?S)?/', $duration, $matches)
I use the $matches array to format my new String. So, in Swift, i found this thread : Swift extract regex matches, which seems to do what I want. But when i get the result, my array is only one String long, with my entire input...
func matchesForRegexInText(regex: String!, text: String!) -> [String] {
let regex = NSRegularExpression(pattern: regex,
options: nil, error: nil)!
let nsString = text as NSString
let results = regex.matchesInString(text,
options: nil, range: NSMakeRange(0, nsString.length)) as [NSTextCheckingResult]
return map(results) { nsString.substringWithRange($0.range)}
}
let matches = matchesForRegexInText("P(([0-9]+)Y)?(([0-9]+)M)?(([0-9]+)D)?T?(([0-9]+)H)?(([0-9]+)M)?(([0-9]+)(.[0-9]+)?S)?", text: "PT00042H42M42S")
println(matches)
// [PT00042H42M42S]
Do you know what's wrong?
Thank you for your answers!