我想構造Regex
但它不起作用。誰能幫忙?如何構建正則表達式模式Swift
我有一個字符串,其中我想刪除以下字符:
* _- + =#:> < & [] \ n上
並且還指示以除去所有的文字(/
和)
之間的代碼是belkow:
if let regex = try? NSRegularExpression(pattern: "&[^*_-=;](\\)*;", options: .CaseInsensitive) {
let modString = regex.stringByReplacingMatchesInString(testString, options: .WithTransparentBounds, range: NSMakeRange(0, testString.characters.count), withTemplate: "")
print(modString)
}
通過https://regex101.com測試你的正則表達式,這是因爲失敗。你確定你的正則表達式嗎? – Larme
@Larme不!這就是爲什麼我在Stack中尋求幫助:) ..我的正則表達式有問題。這是我需要用空格替換* _- + =#:><&[] –
使用Do-Try-Catch帶有一個錯誤變量而不是'try?',並且會出現一個很好的錯誤消息,解釋正則表達式是無效。每日課程:不要忽略錯誤消息。 :) – Moritz