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
。我上网查了一下,发现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
}
在最新的swift 3.0中使用Xcode 8 Beta 6(最新的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
}