我想知道如何从NSTextCheckingResult对象中找到子字符串。到目前为止,我已经尝试过了:
import Foundation {
let input = "My name Swift is Taylor Swift "
let regex = try NSRegularExpression(pattern: "Swift|Taylor", options:NSRegularExpressionOptions.CaseInsensitive)
let matches = regex.matchesInString(input, options: [], range: NSMakeRange(0, input.characters.count))
for match in matches {
// what will be the code here?
}
3条答案
按热度按时间xienkqul1#
试试看:
ymdaylpp2#
下面是Swift 3的代码。它返回一个String数组
所以总体的例子可能是这样的:
wqsoz72f3#
你可以把这段代码放在for循环中,
str
将包含匹配的字符串。let range = match.range let str = (input as NSString).substringWithRange(range)
个