Nick
Nick

Reputation: 498

How can I match this pattern using a regular expression?

I have strings coming into my application. Each incoming string is composed of substrings with no spaces between the substrings. For example, "ID1ID1ID1ID2ID2ID2", where "ID1" is a substring and "ID2" is a substring. I want a regular expression which can detect when there is a pattern where a substring surrounds a set of substrings that does not contain the first substring. So, for example "ID1ID2ID1", "ID1ID1ID3ID1", and "ID1ID2ID3ID2ID1" would all match. Along with the incoming string, I have the first substring in that string (i.e. ID1). So, using that first substring, I want to say "Match Any number of ID1s, then (any string that is not ID1), and then any number of ID1s".

As an example, what I have tried so far is:

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

I've just used regexpal to test this, and it seems to work ok. Is there a better way to do this though? I was looking into using lookarounds, but I couldn't see a way to use them since the lookaround doesn't consume any part of the string. Also, the fact that I am using the [^ID1] doesn't seem right since that just checks to see that the characters I, D, and 1 are not used. Thank you.

Upvotes: 0

Views: 34

Answers (1)

user557597
user557597

Reputation:

If you know its ID1, you can do this

 # (?: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

Upvotes: 1

Related Questions