99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829

Swift 用函數(shù)實(shí)現(xiàn)正則表達(dá)式的庫 SwiftVerbalExpressions

發(fā)布時間:2017-03-06 22:43  回復(fù):0  查看:4034  感興趣:19  贊:1   最后回復(fù):2017-03-06 22:43  

SwiftVerbalExpressions 是一個Swift庫,可以通過函數(shù)的方式實(shí)現(xiàn)復(fù)雜的正則表達(dá)式功能。從JavaScript的版本JSVerbalExpressions移植。

這里有幾個簡單的例子來了解VerbalExpressions的工作原理:

檢查網(wǎng)址是否有效

// Create an example of how to test for correctly formed URLs
let tester = VerEx()
    .startOfLine()
    .then("http")
    .maybe("s")
    .then("://")
    .maybe("www")
    .anythingBut(" ")
    .endOfLine()

// Create an example URL
let testMe = "https://www.google.com"

// Use test() method
if tester.test(testMe) {
    println("We have a correct URL") // This output will fire
}
else {
    println("The URL is incorrect")
}

// Use =~ operator
if testMe =~ tester {
    println("We have a correct URL") // This output will fire
}
else {
    println("The URL is incorrect")
}

println(tester) // Outputs the actual expression used: "^(?:http)(?:s)?(?::\/\/)(?:www)?(?:[^ ]*)$"
替換字符串
let replaceMe = "Replace bird with a duck"

// Create an expression that seeks for word "bird"
let verEx = VerEx().find("bird")

// Execute the expression like a normal RegExp object
let result = verEx.replace(replaceMe, with: "duck")

println(result) // Outputs "Replace duck with a duck"
替換字符串簡寫方式
let result2 = VerEx().find("red").replace("We have a red house", with: "blue")

println(result2) // Outputs "We have a blue house"
API文檔

可以查看JavaScript版本的API文檔


相關(guān)開源代碼

您還未登錄,請先登錄

熱門帖子

最新帖子

?