我想从匹配正则表达式模式的字符串中提取子字符串.
所以我正在寻找这样的东西:
func matchesForRegexInText(regex: String!, text: String!) -> [String] {
???
}
Run Code Online (Sandbox Code Playgroud)
所以这就是我所拥有的:
func matchesForRegexInText(regex: String!, text: String!) -> [String] {
var regex = NSRegularExpression(pattern: regex,
options: nil, error: nil)
var results = regex.matchesInString(text,
options: nil, range: NSMakeRange(0, countElements(text)))
as Array<NSTextCheckingResult>
/// ???
return ...
}
Run Code Online (Sandbox Code Playgroud)
问题是,这matchesInString提供了我的一个数组NSTextCheckingResult,其中NSTextCheckingResult.range的类型的NSRange.
NSRange是不相容的Range<String.Index>,所以它阻止我使用text.substringWithRange(...)
有没有想过如何在没有太多代码的情况下在swift中实现这个简单的事情?
我在这一行遇到编译器错误:
UIDevice.currentDevice().identifierForVendor.UUIDString.substringToIndex(8)
Run Code Online (Sandbox Code Playgroud)
类型'String.Index'不符合协议'IntegerLiteralConvertible'
我的意图是得到子串,但是如何?
我刚刚转换了我的小应用程序,但我发现了这个错误:'substring(from :)'已被弃用:请使用字符串切片下标和'partial range from'运算符
我的代码是:
let dateObj = dateFormatterFrom.date(from: dateStringa)
if dateObj != nil {
cell.detailTextLabel?.text = dateFormatterTo.string(from:(dateObj!))
} else {
let index = thisRecord.pubDate.index(thisRecord.pubDate.startIndex, offsetBy: 5)
cell.detailTextLabel?.text = thisRecord.pubDate.substring(from: index)
}
Run Code Online (Sandbox Code Playgroud) 我在将Swift 3代码转换为Swift 4时遇到了麻烦.我已成功地将应用程序中的所有其他内容成功翻译,但是我遇到了一行代码问题:
cleanURL = cleanURL.substring(to: cleanURL.index(before: cleanURL.endIndex))
Run Code Online (Sandbox Code Playgroud)
我得到的错误是这样的:
ViewController.swift:62:33: 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range upto' operator.
Run Code Online (Sandbox Code Playgroud) 如何将以下代码更新为新版本的swift:
self.areaCodeLael.text! = localNumber.substring(to: localNumber.index(localNumber.startIndex, offsetBy: 3))
Run Code Online (Sandbox Code Playgroud)
我曾尝试过这篇文章,但我无法正确 使用如何在Swift 4中使用字符串切片下标?
我调整了原始代码,localNumber[..<3]但得到了:
无法使用类型为"PartialRangeUpTo"的索引下标"String"类型的值
我正在使用html的解码功能.但是我收到了这个警告.我该如何摆脱?
func decode(_ entity : String) -> Character? {
if entity.hasPrefix("&#x") || entity.hasPrefix("&#X"){
return decodeNumeric(entity.substring(with: entity.index(entity.startIndex, offsetBy: 3) ..< entity.index(entity.endIndex, offsetBy: -1)), base: 16)
} else if entity.hasPrefix("&#") {
return decodeNumeric(entity.substring(with: entity.index(entity.startIndex, offsetBy: 2) ..< entity.index(entity.endIndex, offsetBy: -1)), base: 10)
} else {
return characterEntities[entity]
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
我对 swift 中的 NSRegularExpression 有点困惑,有人可以帮助我吗?
任务:1给出("name","john","name of john")
那么我应该得到["name","john","name of john"]. 在这里我应该避免使用括号。
任务:2给出("name"," john","name of john")
那么我应该得到["name","john","name of john"]. 在这里我应该避免括号和额外的空格,最后得到字符串数组。
任务:3给出key = value // comment
那么我应该得到["key","value","comment"]. 在这里,我应该通过避免只获取行中的字符串,=并且//
我已经尝试了下面的任务 1 代码但没有通过。
let string = "(name,john,string for user name)"
let pattern = "(?:\\w.*)"
do {
let regex = try NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let matches = regex.matches(in: string, options: [], range: NSRange(location: 0, length: string.utf16.count))
for match …Run Code Online (Sandbox Code Playgroud) 当UILabel截断文本时,默认情况下会插入3个点.是否可以更改这些字符或禁用它们?
我试图将Swift 3代码转换为Swift 4.但是这个错误显示:
"substring(from :)'已弃用:请使用字符串切片下标和'partial range from'运算符."
我的代码是:
extension String {
func substring(_ from: Int) -> String {
return self.substring(from: self.characters.index(self.startIndex, offsetBy: from))
}
}
Run Code Online (Sandbox Code Playgroud) 我刚刚在 Xcode 10.1 上将应用程序从swift3转换为swift4.2
我正在修复一些已出现的错误。显然 substring(from:)' 已被弃用。使用字符串切片下标和“部分范围来自”运算符
t_prefix_phone = contact_phone.substring(to:contact_phone.index(contact_phone.startIndex, offsetBy: 3))
t_phone = contact_phone.substring(from:contact_phone.index(contact_phone.endIndex, offsetBy: -7))
Run Code Online (Sandbox Code Playgroud)
您能否帮我将上面的代码翻译为 4.2,使结果仍然是字符串。
谢谢
swift ×8
ios ×4
swift4 ×4
substring ×3
regex ×2
deprecated ×1
iphone ×1
regex-greedy ×1
regex-group ×1
string ×1
swift3 ×1
swift4.2 ×1
truncation ×1
uilabel ×1
xcode ×1
xcode10.1 ×1