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。
答案 0 :(得分:31)
在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
}
答案 1 :(得分:5)
在最新的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
}