在Cocoa中使用NSRegularExpression时出现Cocoa错误2048 [英] Cocoa error 2048 when using NSRegularExpression in Cocoa
问题描述
我正在构建一个正则表达式用于iOS应用程序中的解析器。这是我的代码:
NSRegularExpression * regex =
[NSRegularExpression regularExpressionWithPattern:@(?< =# ^ \r\\\
] * [\r\\\
] +)[^#] [^ \r\\\
] +
选项:NSRegularExpressionAnchorsMatchLines
错误:& regexError
];
if(regexError){
NSLog(@regexError:%@,regexError);
return nil;
}
从此回答。
这会显示此错误:
regexError:Error Domain = NSCocoaErrorDomain Code = 2048无法完成此操作(Cocoa错误2048.)UserInfo = 0x8e86670 {NSInvalidValue =(?< =#EXT [^
Cocoa错误2048是一个 NSFormattingErrorMinimum
根据文档
这是什么意思?解决方案
你试图匹配一个新的行/换行符字符你插入一个文字换行符号到你的正则表达式...你需要改为插入代码换行。尝试转义为 \\\\
等。
编辑: >
您必须转义全部特殊字符串。例如,您希望正则表达式字符串包含 \
+ r
,而不是换行字符。因此,您需要使用 \\r
而不是 \r
。
(?< =#EXT [^ \\r\\\ \\ n] * [\\r\\\\
] +)[^#] [^ \\r\\\\
] +
编辑2:
您的外观不能有无限长度的字符串-背后。因此,不允许 *
和 +
。这是根据 ICU正则表达式参考。 ( NSRegularExpression
使用ICU regex语法。)
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:®exError
];
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?
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.)
这篇关于在Cocoa中使用NSRegularExpression时出现Cocoa错误2048的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!