You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

正则表达式问题:如何匹配>与<之间的第三个单词

Solution to Match the Third Word Between &gt; and &lt;

Got it, let's break this down. Your original regex (?<=&gt;)(\w*)(?=&lt;) works great for grabbing every word wrapped between &gt; and &lt;, but adding {2} directly didn't hit the mark because you weren't accounting for the full surrounding structure of the first two matches. Here's how to fix it:

The Working Regex

(?:&gt;\w*&lt;){2}&gt;(\w*)&lt;

Let's Break It Down

  • (?:&gt;\w*&lt;){2}: This is a non-capturing group ((?:...)) that matches the full &gt;WORD&lt; structure exactly 2 times. The non-capturing part means we don't save these first two words—we just skip over them.
  • &gt;(\w*)&lt;: After skipping the first two matches, this targets the third occurrence. The parentheses around \w* capture the word we actually want.

Example Usage

If your input string is:

Lorem&gt;First&lt;Ipsum&gt;Second&lt;Dolor&gt;Third&lt;Sit

Running the regex will capture Third as the first (and only) capturing group value.

Why Your Previous Attempts Failed

  • Adding {2} to the lookbehind ((?<=&gt;){2}) would look for two consecutive &gt; characters, which isn't your structure.
  • Adding {2} to the lookahead ((?=&lt;){2}) would require two consecutive &lt; characters, which also doesn't match your pattern.
  • You need to match the entire &gt;WORD&lt; block for the first two instances before targeting the third.

Alternative (If You Prefer Lookarounds)

If you specifically want to stick with lookarounds (though the above method is more straightforward), you can use a positive lookbehind that checks for the first two full blocks:

(?<=(?:&gt;\w*&lt;){2}&gt;)(\w*)(?=&lt;)

This works by verifying the word is preceded by two complete &gt;WORD&lt; blocks plus a &gt;, then followed by &lt;.

内容的提问来源于stack exchange,提问作者Fayaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:40:00