如何使用正则表达式匹配此模式?

How can I match this pattern using a regular expression?

我的应用程序中有字符串。每个传入的字符串都由子字符串组成,子字符串之间没有空格。例如,"ID1ID1ID1ID2ID2ID2",其中 "ID1" 是子字符串,"ID2" 是子字符串。我想要一个正则表达式,它可以检测何时存在一个子字符串围绕一组不包含第一个子字符串的子字符串的模式。因此,例如 "ID1ID2ID1"、"ID1ID1ID3ID1" 和 "ID1ID2ID3ID2ID1" 都会匹配。除了传入的字符串,我还有该字符串中的第一个子字符串(即 ID1)。因此,使用第一个子字符串,我想说 "Match Any number of ID1s, then (any string that is not ID1), and then any number of ID1s".

举个例子,目前我试过的是:

.*ID1.*[^ID1]+ID1.*

我刚刚使用正则表达式对此进行了测试,它似乎工作正常。有没有更好的方法来做到这一点?我正在研究使用环视,但我看不到使用它们的方法,因为环视不使用字符串的任何部分。此外,我使用 [^ID1] 的事实似乎并不正确,因为它只是检查字符 I、D 和 1 是否未被使用。谢谢。

如果你知道它 ID1,你可以这样做

 # (?:ID1)+(?:(?!ID1|[ ]).)+(?:ID1)+

 (?:                           # Cluster group, leading 'ID1' 
      ID1                           # An 'ID1'
 )+                            # End cluster, do 1 to many times

 (?:                           # Cluster group
      (?!                           # Lookahead NEGAGIVE assertion
           ID1                           # 'ID1'
        |  [ ]                           # or space
      )                             # End lookahead
      .                             # Assertion passed, grab any character
 )+                            # End cluster, do 1 to many times

 (?:                           # Cluster group, trailing 'ID1'
      ID1                           # An 'ID1'
 )+                            # End cluster, do 1 to many times