在Swift 3.0中使用String.range

let us = "http://example.com" let range = us.rangeOfString("(?<=://)[^.]+(?=.com)", options:.RegularExpressionSearch) if range != nil { let found = us.substringWithRange(range!) print("found: \(found)") // found: example } 

这段代码在Swift 2中提取了反斜杠和点com之间的substring 。我search了Internet,发现rangeOfString变成了range()

但是我仍然无法使代码在Swift 3.0中工作。 你可以帮帮我吗 ?

编辑:我使用SWIFT 3 07-25构build。

在swift 3.0中, rangeOfString语法像这样改变了。

 let us = "http://example.com" let range = us.range(of:"(?<=://)[^.]+(?=.com)", options:.regularExpression) if range != nil { let found = us.substring(with: range!) print("found: \(found)") // found: example } 

在使用Xcode 8 Beta 6的最新swift 3.0(SDK的最新更新)中:

 let us = "http://example.com" let range = us.range(of: "(?<=://)[^.]+(?=.com)", options: .regularExpression) if range != nil { let found = us.substring(with: range!) print("found: \(found)") // found: example }