【发布时间】:2021-08-03 16:50:24
【问题描述】:
我需要创建一个谓词来查找以下字符串:
"fred\n5" 其中 \n 是换行符。
至少,这是读取元数据时返回的字符串
【问题讨论】:
标签: swift newline nspredicate
我需要创建一个谓词来查找以下字符串:
"fred\n5" 其中 \n 是换行符。
至少,这是读取元数据时返回的字符串
【问题讨论】:
标签: swift newline nspredicate
你可以用正则表达式来做到这一点
let string = """
fred
5
"""
let predicate = NSPredicate(format: "self MATCHES %@", "fred\\n5")
predicate.evaluate(with: string) // true
也可以使用fred(\\n|\\r)5 模式,它同时考虑换行和返回。
或者删除换行符(实际上是任何空白和换行符)
let trimmedString = string.replacingOccurrences(of: "\\s", with: "", options: .regularExpression)
【讨论】:
.+\\d$