正则表达式用于两个模式之间的单个定界符

时间:2018-10-28 17:47:59

标签: regex pcre

给出一个类似@1=A1@2=A2@3=A3>>@1=B1@2=B2@3=B3>>@1=C1@2=C2@3=C3>>@1=D1@2=D2@3=D3的输入,我想匹配@ 2 = A2和@ 2 = B2,使得它们之间只有一个>>

我尝试了正则表达式(?!@2=A2.*>>.*>>.*@2=B2)@2=A2@.*>>.*@2=B2

这可以正确识别输入,例如 @1=A1@2=A2@3=A3>>@1=B1@2=B2@3=B3>>@1=C1@2=C2@3=C3>>@1=D1@2=D2@3=D3,也忽略输入 @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在@ 2 = A2之后,但@ 2 = C2在两者之间。)。

但是,它在诸如@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 个答案:

答案 0 :(得分:4)

怎么样:

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

DEMO

说明:

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