Cocoa error 2048 when using NSRegularExpression in Cocoa

℡╲_俬逩灬. 提交于 2019-11-28 04:38:45

问题


I'm building a regular expression for use in a parser in an iOS app. Here's my code:

NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:@"(?<=#EXT[^\r\n]*[\r\n]+)[^#][^\r\n]+"
                                          options:NSRegularExpressionAnchorsMatchLines
                                            error:&regexError
 ];
if (regexError) {
    NSLog(@"regexError: %@", regexError);
    return nil;
}

From this answer.

This gives out this error:

regexError: Error Domain=NSCocoaErrorDomain Code=2048 "The operation couldn’t be completed. (Cocoa error 2048.)" UserInfo=0x8e86670 {NSInvalidValue=(?<=#EXT[^

Cocoa error 2048 is an NSFormattingErrorMinimum according to the docs... But there's literally no further explanation.

What does it mean?


回答1:


are you trying to match a new line/line feed character? you've inserted a literal new line character into your regex... you need to instead insert the code for a newline. Try escaping as \\n etc.

edit:

You have to escape all special strings. For example you want your regex string to contain \+r, not a linefeed character. So you need to use \\r instead of \r.

i.e.

"(?<=#EXT[^\\r\\n]*[\\r\\n]+)[^#][^\\r\\n]+"

edit 2:

You cannot have unlimited length strings in your look-behind. So, no * and no + allowed. This is per the ICU regex reference. (NSRegularExpression uses ICU regex syntax.)



来源:https://stackoverflow.com/questions/20640375/cocoa-error-2048-when-using-nsregularexpression-in-cocoa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!