Reputation: 2642
I have this code working in Objective-C
NSRegularExpression* regex = [[NSRegularExpression alloc] initWithPattern:@"(.*?)(<[^>]+>|\\Z)" options:NSRegularExpressionCaseInsensitive|NSRegularExpressionDotMatchesLineSeparators error:nil];
NSArray* results = [regex matchesInString:text options:NSMatchingReportProgress range:NSMakeRange(0, text.length)];
But the Swift version is not working. The matchesInString
is returning an empty Array
(using the same tagged text in both cases)
let regexOptions = NSRegularExpressionOptions.CaseInsensitive | NSRegularExpressionOptions.DotMatchesLineSeparators
let regex = NSRegularExpression.regularExpressionWithPattern("(.*?)(<[^>]+>|\\Z)", options: regexOptions, error: nil)
var results = regex.matchesInString(markupText, options: nil, range: NSMakeRange(0, countElements(markupText))) as Array<NSTextCheckingResult>
Even though the documentation states that the matchesInString
returns an Array
of NSTextCheckingResult
, I noted that in the Objective-C code (that works) the Array
contains NSSimpleRegularExpressionCheckingResult
objects and not NSTextCheckingResult
objects. The empty Array
reported in the Swift version reports zero NSTextCheckingResult
objects though
Any idea of what is missing here?
Upvotes: 0
Views: 1407
Reputation: 34935
Update, this works for me:
let regexOptions:NSRegularExpressionOptions?
= NSRegularExpressionOptions.CaseInsensitive
var matchingError : NSError?
let regex = NSRegularExpression(pattern: "(.*?)(<[^>]+>|\\Z)",
options: regexOptions!, error: &matchingError)
let markupText = "<html></html>"
let results = regex.matchesInString(markupText, options: nil,
range: NSMakeRange(0, countElements(markupText))) as Array<NSTextCheckingResult>
I am not sure why regexOptions has to be an optional. May be a bug in the compiler.
Older thoughts
This may be a bug in Xcode6b2. I simplified your example to just one line:
let regexOptions = NSRegularExpressionOptions.CaseInsensitive
And I get this error:
fatal error: Can't unwrap Optional.None
I would file a bug for this.
Upvotes: 4