ios - How to use NSRegularExpression in Swift 2.0 in xcode 7 -
//the error here let regex = nsregularexpression(pattern: "(<img.*?src=\")(.*?)(\".*?>)", options: nil, error: nil)
//the error is:***
cannot find initializer type nsregularexpression accept argument of type (pattern: string,ption:nil,error:nil)
there 2 changes regards syntax in swift 2.0: (1) wrap call in try ... catch
block instead of supplying error
parameter; , (2) options
should set
, not numerical or
of individual options.
in case code should this:
do { let regex = try nsregularexpression(pattern: "(<img.*?src=\")(.*?)(\".*?>)", options: []) } catch let error nserror { print(error.localizeddescription) }
if know pattern succeeds, can shorten this:
let regex = try! nsregularexpression(pattern: "(<img.*?src=\")(.*?)(\".*?>)", options: [])
now if want set options pattern, can this:
let regex = try! nsregularexpression(pattern: "(<img.*?src=\")(.*?)(\".*?>)", options: [.caseinsensitive, .anchorsmatchlines])
Comments
Post a Comment