【发布时间】:2015-07-07 02:35:48
【问题描述】:
我正在尝试使 NSRegularExpression 使用捕获组模式。我有字符串abc{=2} 和正则表达式模式{(.+)} 来获取=2 字符串,但是每次创建NSRegularExpression 实例的尝试都以
Optional(Error Domain=NSCocoaErrorDomain Code=2048 "The operation couldn’t be completed. (Cocoa error 2048.)" UserInfo=0x7fad7253cb40 {NSInvalidValue={(.+)}})
代码:
var error: NSError?
let regexp = NSRegularExpression(pattern: "{(.+)}", options: .CaseInsensitive, error: &error)
println("error = \(error)")
我找不到这种模式有什么问题。我正在使用 regex101.com 页面,它在那里工作。
提前谢谢你!
编辑
我看到转义大括号 (\\{(.+)\\}) 解决了错误问题,但后来我得到了 {=2} 而不是 =2。
【问题讨论】:
-
您的答案应该有效。但这不是标准的。许多语言等甚至不允许您进行环视,但大多数允许捕获组。
标签: regex swift nsregularexpression