Regex for single delimiter between two patterns

Viewed 58

Given an input like @1=A1@2=A2@3=A3>>@1=B1@2=B2@3=B3>>@1=C1@2=C2@3=C3>>@1=D1@2=D2@3=D3, I want to match @2=A2 and @2=B2 such that there is only one >> between them.

I tried the regex (?!@2=A2.*>>.*>>.*@2=B2)@2=A2@.*>>.*@2=B2.

This correctly identifies input like @1=A1@2=A2@3=A3>>@1=B1@2=B2@3=B3>>@1=C1@2=C2@3=C3>>@1=D1@2=D2@3=D3 and also ignores the input @1=A1@2=A2@3=A3>>@1=C1@2=C2@3=C3>>@1=B1@2=B2@3=B3>>@1=D1@2=D2@3=D3(@2=B2 comes after @2=A2 but @2=C2 is in between.).

However, it fails on inputs like @1=A1@2=A2@3=A3>>@1=B1@2=B2@3=B3>>@1=B1@2=B2@3=B3>>@1=D1@2=D2@3=D3.

1 Answers

How about:

@2=A2[^>]*>>[^>]*@2=B2

DEMO

Explanation:

@2=A2       # literally
[^>]*       # 0 or more any character that is not >
>>          # literally
[^>]*       # 0 or more any character that is not >
@2=B2       # literally
Related