//: Playground - noun: a place where people can play
import UIKit
import Foundation
let beforeText = "途牛旅游網于2006年10月創立于南京,以“讓旅游更簡單”為使命,為消費者提供由北京、天津、上海、廣州、深圳、南京等64個城市出發的旅游產品預訂服務,產品全面,價格透明,全年365天24小時400電話預訂,并提供豐富的后續服務和保障。\n 途牛旅游網提供8萬余種旅游產品供消費者選擇,涵蓋跟團、自助、自駕、郵輪、酒店、簽證、景區門票以及公司旅游等,已成功服務累計超過400萬人次出游。2014年12月15日,途牛旅游網宣布與弘毅投資、京東商城、攜程旗下子公司“攜程投資”以及途牛首席執行官與首席運營官簽訂股權認購協議。根據協議途牛將向上述投資者出售1.48億美元的新發行股份。[1]\n 2015年11月24日,途牛旅游網與海航旅游集團共同宣布戰略結盟。海航旅游戰略投資途牛5億美元,雙方將利用各自優質資源,在線上旅游、航空、酒店服務等領域開展深度合作。[2]易觀報告顯示,2015年第3季度,途牛交易規模達到46.5億元人民幣,同比增長141.1%。這已是途牛連續三個季度同比增速超過三位數:2015年第二季度時,途牛交易規模同比增長135.3%;第一季度,這一數據為122.8%。\n 從增速來看,途牛再度蟬聯行業第一。[3]以全年業績來看,途牛2015年的凈收入為76億元人民幣(合11.802億美元),較2014年增長116.3%。收入增長主要來自于跟團游、自助游及其他收入的增長。2015年總出游人次為4449053,較2014年的2181834人次增長103.9%。[4]adfasdfadf"
let range = NSMakeRange(0, beforeText.characters.count)
let regexOption : NSRegularExpression.Options = .caseInsensitive
let pattern = "途牛"
let regex = try? NSRegularExpression(pattern: pattern, options: regexOption)
//替換
func replaceString()
{
guard regex != nil else {
return;
}
//匹配整個range
var afterText = regex!.stringByReplacingMatches(in: beforeText, options: .withTransparentBounds, range: range, withTemplate: "《途牛》")
//只匹配開頭
afterText = regex!.stringByReplacingMatches(in: beforeText, options: .anchored, range: range, withTemplate: "《途?!?)
}
replaceString()
func otherUses() {
guard regex != nil else {
return;
}
let matchNum = regex!.numberOfMatches(in: beforeText, options: .withTransparentBounds, range: range)
print(matchNum)
let firstCheckingResult = regex!.firstMatch(in: beforeText, options: .withTransparentBounds, range: range)
print(firstCheckingResult ?? "")
let firstRange = regex!.rangeOfFirstMatch(in: beforeText, options: .withTransparentBounds, range: range)
if !NSEqualRanges(NSMakeRange(NSNotFound, 0), firstRange) {
print(String(describing: firstRange))
}
let allMatch = regex!.matches(in: beforeText, options: .withTransparentBounds, range: range)
for ele in allMatch {
print(String.init(describing: ele))
print((beforeText as NSString).substring(with: ele.range))
}
}
otherUses()
//找沒找到都會調用 ,即是完成一次匹配就好調用
func searchTextWithProgressOption()
{
guard regex != nil else {
return;
}
var count = 0
regex!.enumerateMatches(in: beforeText, options: .reportProgress, range: range, using: { (result, matchFlag, stop) in
if result?.numberOfRanges ?? 0 > 0
{
print(String.init(describing: result))
}
print(String(describing: matchFlag))
print(count)
count += 1
if count == 10
{//中斷匹配
// stop.pointee = true
}
})
}
searchTextWithProgressOption()
//找到后才會調用
func searchTextWithCompletionOption() {
guard regex != nil else {
return;
}
var count = 110
regex!.enumerateMatches(in: beforeText, options: .reportCompletion, range: range, using: { (result, matchFlag, stop) in
/*
the result 可能是nil
*/
// if result?.numberOfRanges ?? 0 > 0
// {
// print(String.init(describing: result))
// }
print(String.init(describing: result))
print(matchFlag)
print(count)
count -= 1
if count == 0
{
//stop.pointee = true
}
})
}
searchTextWithCompletionOption()
//校驗
func verify() {
let patterns = ["^[a-z]{1,10}$", // First name
"^[a-z]$", // Middle Initial
"^[a-z']{2,10}$", // Last Name
"^(0[1-9]|1[012])[-/.](0[1-9]|[12][0-9]|3[01])[-/.](19|20)\\d\\d$" ] // Date of Birth
let textFields = [ "Xiong11", "D", "Wei", "01-07-1990" ]
let regexes = patterns.map {
try? NSRegularExpression(pattern: $0, options: .caseInsensitive)
}
for index in 0..<textFields.count {
let checkResult = regexes[index]?.firstMatch(in: textFields[index], options: .withTransparentBounds, range: NSMakeRange(0, textFields[index].characters.count))
guard checkResult != nil else {
print("\(textFields[index]) is not valide")
continue
}
if !NSEqualRanges((checkResult?.range)!, NSMakeRange(NSNotFound, 0)) {
print("\(textFields[index]) is valide")
}
else
{
print("\(textFields[index]) is not valide")
}
}
}
verify()
//示例
func example() {
let pattern = "\\{\\{([a-z1-9\\s]+):([a-z1-9\\s]+)\\}\\}"
let textField = ["{{context:function}}","{{context1: function}}","{{ context2 : function }}"]
let regex = try? NSRegularExpression.init(pattern: pattern, options: .caseInsensitive)
guard regex != nil else {
print("regex is nil")
return
}
for str in textField {
let result = regex?.matches(in: str, options: .withTransparentBounds, range: NSMakeRange(0, str.characters.count))
guard result != nil else {
continue
}
for ele in result! {
print(ele.numberOfRanges)
for index in 0..<ele.numberOfRanges {
print((str as NSString).substring(with: ele.rangeAt(index)))
}
}
}
}
example()
NSRegular
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。