看看正则表达式背后

RMa*_*lke 3 c# regex

我从正则表达式开始(总是从我需要的网上使用)

我需要一些给出输入的东西:

Input: AAABBBCCC
Index: 012345678
Run Code Online (Sandbox Code Playgroud)

正则表达式匹配将是:

  • AA从0,1
  • 从1,2开始的AA(即使已经消耗了1中的A)
  • BB来自3,4
  • BB从4,5(即使4中的B已被消耗)
  • CC来自6,7
  • 来自7,8的CC(即使已经消耗了7中的B)

我现在的正则表达式是(A{2}|B{2}|C{2}).这不是我真正的问题,但是对于As,Bs和Cs我有不同的工作正则表达式.

我认为我应该使用一些look behind运算符但是尝试:((A{2}|B{2}|C{2})$1)或者(?<=(A{2}|B{2}|C{2}))不会工作.

这是一个例子.

注意:我的问题在于c#,如果重要的话

Tim*_*ker 5

你确实需要环顾四周,但我会用一个积极的先行断言:

(?=(([ABC])\2))
Run Code Online (Sandbox Code Playgroud)

您的匹配结果将包含在match.Groups(1)每个match对象中.

说明:

(?=       # Look ahead to check that the following matches:
 (        # Match and capture in group number 1:
  (       # Match and capture in group number 2:
   [ABC]  # Any letter A, B or C
  )       # End of capturing group 2
  \2      # Now match that same letter again.
 )        # End of group 1. It now contains AA, BB or CC
)         # End of lookahead assertion
Run Code Online (Sandbox Code Playgroud)

更简单的解决方案:

(?=(AA|BB|CC))
Run Code Online (Sandbox Code Playgroud)