菜鸟教程小白 发表于 2022-12-11 17:26:25

ios - 在 Cocoa 中使用 NSRegularExpression 时出现 Cocoa 错误 2048


                                            <p><p>我正在构建一个用于 iOS 应用程序解析器的正则表达式。这是我的代码:</p>

<pre><code>NSRegularExpression *regex =
*[\r\n]+)[^#][^\r\n]+&#34;
                                          options:NSRegularExpressionAnchorsMatchLines
                                          error:&amp;regexError
];
if (regexError) {
    NSLog(@&#34;regexError: %@&#34;, regexError);
    return nil;
}
</code></pre>

<p>来自 <a href="https://stackoverflow.com/questions/20633416/regex-match-line-if-previous-line-satisfies-a-criteria#comment30894792_20633416" rel="noreferrer noopener nofollow">this answer</a> .</p>

<p>这给出了这个错误:</p>

<p><code>regexError: Error Domain=NSCocoaErrorDomain Code=2048 “操作无法完成。(Cocoa 错误 2048。)” UserInfo=0x8e86670 {NSInvalidValue=(?<=#EXT[^</code></p>

<p>Cocoa 错误 2048 是 <code>NSFormattingErrorMinimum</code> <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html" rel="noreferrer noopener nofollow">according to the docs</a> ...但实际上没有进一步的解释。 </p>

<p>这是什么意思? </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您是否要匹配新的换行符/换行符?你已经在你的正则表达式中插入了一个文字换行符......你需要插入换行符的代码。尝试转义为 <code>\\n</code> 等。</p>

<p><strong>编辑:</strong></p>

<p>您必须转义 <em>所有</em> 特殊字符串。例如,您希望您的正则表达式字符串包含 <code>\</code>+<code>r</code>,而不是换行符。所以你需要使用 <code>\\r</code> 而不是 <code>\r</code>。</p>

<p>即</p>

<pre><code>&#34;(?&lt;=#EXT[^\\r\\n]*[\\r\\n]+)[^#][^\\r\\n]+&#34;
</code></pre>

<p><strong>编辑 2:</strong></p>

<p>您的后视中不能有无限长度的字符串。因此,不允许 <code>*</code> 和 <code>+</code>。这是根据 <a href="http://userguide.icu-project.org/strings/regexp" rel="noreferrer noopener nofollow">ICU regex reference</a> . (<code>NSRegularExpression</code> 使用 ICU 正则表达式语法。)</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 Cocoa 中使用 NSRegularExpression 时出现 Cocoa 错误 2048,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39325090/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39325090/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 Cocoa 中使用 NSRegularExpression 时出现 Cocoa 错误 2048