我想看看特定的字符串是否與swift中的正則表達式模式匹配。我到目前爲止的代碼似乎檢查給定字符串中的任何子字符串是否與 正則表達式匹配。如何查看特定字符串是否與正則表達式匹配?
let string = " (5"
let pattern = "[0-9]"
if string.rangeOfString(pattern, options: .RegularExpressionSearch) != nil {
print("matched")
}
上面的代碼返回一個匹配,即使字符串作爲一個整體不匹配模式。
如何我想代碼操作:
" (5" -> no match
"(5" -> no match
"5" -> match
代碼currenty如何操作:範圍
" (5" -> match
"(5" -> match
"5" -> match
'讓模式= 「^ [0-9] $」' –