且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在正则表达式匹配重叠

更新时间:2022-11-12 08:52:58

一个可能的解决办法是使用的背后正面看:

A possible solution could be to use a positive look behind:

(?<=n)n

这会给你的终点位置:

It would give you the end position of:


  1. N N NN
    &NBSP;

  2. N * N *的 N ñ
    &NBSP;

  3. NN * N *的 N

  1. nnnn  
  2. n*n*nn  
  3. nn*n*n


作为mentionned由蒂莫西·扈利中,一个正向前查找更直观

As mentionned by Timothy Khouri, a positive lookahead is more intuitive

我想preFER他主张(= NN?)N 的简单的形式:

I would prefer to his proposition (?=nn)n the simpler form:

(n)(?=(n))

这将引用第一弦您想要的位置并会捕捉组第二n(2)

That would reference the first position of the strings you want and would capture the second n in group(2).

这是因为:


  • 任何有效的常规前pression可以超前内部使用。

  • 如果它包含捕获括号,在反向引用将保存

  • Any valid regular expression can be used inside the lookahead.
  • If it contains capturing parentheses, the backreferences will be saved.

因此​​,组(1),组(2)将捕捉一切'N'重新presents(即使它是一个复杂的正则表达式)。

So group(1) and group(2) will capture whatever 'n' represents (even if it is a complicated regex).