菜鸟教程小白 发表于 2022-12-11 22:19:49

ios - RegEx 在 RegexR 中在线工作,但在 NSRegularExpression 中无法在 iOS 上初始化


                                            <p><p>我有一个正则表达式,旨在从 CocoaPods 定义中提取 git URL。 </p>

<p>输入文字如下:</p>

<pre><code>pod &#39;Alamofire&#39;, :git =&gt; &#39;https://github.com/Alamofire/Alamofire.git&#39;, :branch =&gt; &#39;dev&#39;
</code></pre>

<p>正则表达式如下:</p>

<pre><code>(?&lt;=(&#39;Alamofire&#39;.*:git =&gt; &#39;))+(?=(&#39;{1}))
</code></pre>

<p>此正则表达式在 RegexR 上正常工作,请参阅 <a href="https://regexr.com/49o3h" rel="noreferrer noopener nofollow">here</a> ,但是当尝试用它初始化 <code>NSRegularExpression</code> 时,会抛出一个错误,代码 2048 表明该模式无效。通常这是由于缺乏逃生,但这里没有。即使在搜索了 iOS 使用的引擎 ICU 正则表达式文档后,我也无法找出问题所在。</p>

<p>TIA,任何想法都会受到欢迎。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您不能在带有 ICU 正则表达式的后向模式中使用长度未知的模式。您的模式在后视中包含 <code>.*</code>,因此它是无效的 <a href="http://userguide.icu-project.org/strings/regexp" rel="noreferrer noopener nofollow">ICU regexp</a> (请参阅 <em>由后视模式匹配的可能字符串的长度不能无界(没有 <code>*</code> 或 <code>+</code> 运算符。)</em> ICU 后视文档部分)。 </p>

<p>有两种方式:</p>

<ul>
<li>将 <code>.*</code> 替换为 <code>.{0,x}</code> 其中 <code>x</code> 是您希望分隔左侧的最大字符数从右侧模式来看,ICU 正则表达式后视允许 <em>limiting</em>(或 <em>interval</em>,<em>range</em>)量词,这就是为什么它们也被称为“约束宽度”)</li>
<li>重新修改您的模式以使用消耗模式而不是环顾四周,使用捕获括号包装您需要提取的部分,并修改您的代码以获取第 1 组值。</li>
</ul>

<p>这里是方法2,推荐:</p>

<pre><code>let str = &#34;pod &#39;Alamofire&#39;, :git =&gt; &#39;https://github.com/Alamofire/Alamofire.git&#39;, :branch =&gt; &#39;dev&#39;&#34;
let rng = NSRange(location: 0, length: str.utf16.count)
let regex = try! NSRegularExpression(pattern: &#34;&#39;Alamofire&#39;.*:git\\s*=&gt;\\s*&#39;([^&#39;]+)&#39;&#34;)
let matches = regex.matches(in: str, options: [], range: rng)
let group1 = String(str.range(at: 1), in: str)!])
print(group1) // =&gt; https://github.com/Alamofire/Alamofire.git
</code></pre>

<p>见 <a href="https://regex101.com/r/C1WMM3/1" rel="noreferrer noopener nofollow">regex demo</a> ,绿色突出显示的子字符串是您在第 1 组中获得的值。</p>

<p>图案细节:</p>

<ul>
<li><code>'Alamofire'</code> - 文字字符串</li>
<li><code>.*</code> - 除换行符之外的任何 0+ 个字符,尽可能多(替换为 <code>.*?</code> 以尽可能少地匹配)</li >
<li><code>:git</code> - 文字子串</li>
<li><code>\s*=>\s*</code> - 用 0+ 个空格包裹的 <code>=></code> 子字符串</li>
<li><code>'([^']+)'</code> - <code>'</code>,然后是匹配 <code>'</code> 以外的 1+ 个字符的捕获组 #1 和然后是 <code>'</code> 字符。</li>
</ul></p>
                                   
                                                <p style="font-size: 20px;">关于ios - RegEx 在 RegexR 中在线工作,但在 NSRegularExpression 中无法在 iOS 上初始化,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/55038991/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/55038991/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - RegEx 在 RegexR 中在线工作,但在 NSRegularExpression 中无法在 iOS 上初始化