我有這樣的文字中,我需要在第一線正則表達式只返回數字上升到一個新行字符
+1-415-655-0001 US TOLL Access code: 197 703 792
的正則表達式我有隻提取數字只是提取所有數字 /d+
var noteStr = "1-415-655-0001 US TOLL\n\nAccess code: 197 703 792"
findCodeInWord()
func findCodeInWord() -> String?
{
let regex = try! NSRegularExpression(pattern: "\\d+", options: [])
var items = [String]()
regex.enumerateMatchesInString(noteStr, options: [], range: NSMakeRange(0, noteStr.characters.count)) { result, flag, stop in
guard let match = result else {
// result is nil
return
}
let range = match.rangeAtIndex(0)
var matchStr = (noteStr as NSString).substringWithRange(range)
print(matchStr)
}
return items.joinWithSeparator("")
}
但是這將返回所有數字。我只希望它返回14156550001
使用'[^ 0-9] +'因爲你的正則表達式和替換字符串爲空。我不知道斯威夫特但顯然有一個函數,該函數。見http://stackoverflow.com/questions/28503449/swift-replace-substring-regex – shawnt00