Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
964 views
in Technique[技术] by (71.8m points)

ios - Cocoa error 2048 when using NSRegularExpression in Cocoa

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

NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:@"(?<=#EXT[^
]*[
]+)[^#][^
]+"
                                          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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 .

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...