當我使用下面的代碼剝離http://
和www.
掀起了URL的開始,我得到一個錯誤。爲什麼我的Swift中的正則表達式不能編譯?我如何給它一個可變的字符串呢?
var error: NSError? = nil
let regex = NSRegularExpression(pattern: "^(http(s)?://)?(www(\\d)?\\.)?", options: nil, error: &error)
var stringy = "http://www.google.com/"
regex.replaceMatchesInString(stringy, options: nil, range: NSMakeRange(0, countElements(stringy)), withTemplate: "")
錯誤時正在:
'的NSString' 不是 '的NSMutableString'
如何解決這個亞型?我究竟做錯了什麼?
[* * * * replaceMatcehsInString'工作](https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html#//apple_ref/occ/instm/NSRegularExpression/replaceMatchesInString:選項:範圍:withTemplate :)?爲什麼它*不接受不可變的字符串?它與「正則表達式[..]不編譯」無關。與'stringByReplacingMatchesInString'比較。 – user2864740 2014-10-08 01:23:44