我在Swift 3中使用了这样的regEx方式
let reg = try NSRegularExpression(pattern: pattern, options: .CaseInsensitive)
let matches = reg.matchesInString(text, options: NSMatchingOptions.WithTransparentBounds, range: NSMakeRange(0, text.characters.count))然后我使用"<hh>(.*?)</hh>“这样的模式,然后我得到了匹配结果,它的开始部分包含"<hh>”,结束部分包含"</hh>“。如何在它们之间只获得价值?
发布于 2016-06-03 22:22:48
我现在不能测试它,但可以尝试一些类似的东西:
for match in matches {
let matchRange = match.rangeAtIndex(1)
let substring = text.substringWithRange(matchRange)
//do something with your subscring
}发布于 2016-06-03 22:34:39
解决方法是:在用于查找正则表达式匹配的函数中添加一个removingTagLength参数,并在调用substringWithRange时在range中使用此参数
nsText.substringWithRange(NSMakeRange(match.range.location + removingTagLength, match.range.length - (removingTagLength > 0 ? removingTagLength + 1 : 0)))https://stackoverflow.com/questions/37616683
复制相似问题