//方案1
let keyWordRange = allText.range(of: "测试")
//方案2
//let keyWordRange = allText.range(of:"测试", options: .backwards)//反向检索
//方案3
//let keyWordRange = allText.range(of:"day", options: .caseInsensitive, range:nil , locale:nil)//忽略大小写
//方案4
//let set = CharacterSet(charactersIn: "测试")
//let keyWordRange: Range<String.Index>? = allText.rangeOfCharacter(from: set)
guard let keyWordRange = keyWordRange else {
print("关键字未找到。。。")
return
}
let keyWordStart = keyWordRange.lowerBound
let keyWordEnd = keyWordRange.upperBound
let keyWordStartIndex = keyWordStart.utf16Offset(in: allText)
let keyWordEndIndex = keyWordEnd.utf16Offset(in: allText)
print("起始序号:\(keyWordStartIndex)---结束序号:\(keyWordEndIndex)")
let tokeyText = allText[..<keyWordStart]
print("截取关键字之前的字符串:\(tokeyText)")
let fromkeyText = allText[keyWordEnd...]
print("截取关键字之后的字符串:\(fromkeyText)")
let endIndex = allText.endIndex.utf16Offset(in: allText) - 1
let relEnd = String.Index(utf16Offset: endIndex, in: allText)
let fromKeyToEndReduceOne = allText[keyWordEnd..<relEnd]
print("关键字到最后少一位的:\(fromKeyToEndReduceOne)")
////正则表达式获取符合规则的字符串 方案1
func regexFindString() {
let allText = "http://lifusc148387@qq.com.jpg"
let rangeindex = allText.range(of: "[0-9]{4}", options: .regularExpression, range: allText.startIndex..<allText.endIndex, locale:Locale.current)
let findText = allText.substring(with: rangeindex!)
print("正则查找字符串:\(findText)") //输出;1483
//正则表达式获取符合规则的字符串
let rangeindex2 = allText.range(of: ".\\w*$", options: .regularExpression, range: allText.startIndex..<allText.endIndex, locale:Locale.current)
let findText2 = allText.substring(with: rangeindex2!)
print("正则查找文件格式:\(findText2)")
let findText3 = regexGetSub(pattern: ".\\w*$", str: allText)
print("正则查找文件格式3:\(findText3)")
}
/**
正则表达式获取符合规则的字符串 方案2
- parameter pattern: 一个字符串类型的正则表达式
- parameter str: 需要比较判断的对象
- imports: 这里子串的获取先转话为NSString的[以后处理结果含NS的还是可以转换为NS前缀的方便]
- returns: 返回目的字符串结果值数组(目前将String转换为NSString获得子串方法较为容易)
- warning: 注意匹配到结果的话就会返回true,没有匹配到结果就会返回false
*/
func regexGetSub(pattern:String, str:String) -> [String] {
var subStr = [String]()
let regex = try! NSRegularExpression(pattern: pattern, options:[NSRegularExpression.Options.caseInsensitive])
let results = regex.matches(in: str, options: NSRegularExpression.MatchingOptions.init(rawValue: 0), range: NSMakeRange(0, str.count))
//解析出子串
for rst in results {
let nsStr = str as NSString //可以方便通过range获取子串
subStr.append(nsStr.substring(with: rst.range))
//str.substring(with: Range<String.Index>) //本应该用这个的,可以无法直接获得参数,必须自己手动获取starIndex 和 endIndex作为区间
}
return subStr
}